We are about to switch to a new forum software. Until then we have removed the registration on this forum.
so im working on a program and need to import images into the class but also use objects, so i am trying to display the object which is the image but i keep getting a nullpointerexception... i havent used objects in this way before so i dont really have an idea of what im doing or where the npe error could be.. if anyone could help that would be great because being stuck on code this early really sucks...
here is my main class
//lab 4
Boat boat; //calls the boat object
void setup(){
size(600,400);
background(255);
}
void draw(){
boat.setLocation(mouseX,mouseY);
boat.display();
}
and here is the class for the object
class Boat {
PImage boat;
float x,y;
void ship() {
}
void setLocation(float tempX, float tempY) {
x= tempX;
y= tempY;
}
void display() {
image(boat, 5, 50);
}
}
also it is showing the error on line 12 of the main class
addon have another question and was told to post it here
so im back again, i need to use an array to display an image and cant really seem to get it currently i have a if mousePressed statement because im not sure how to initialize the if statement to loop the array i guess?
here is what i have
main class
PImage img_boat;
PImage img_Iceberg;
Boat my_boat;
Iceberg[] my_iceberg;
float mouseX=50;
float mouseY;
float posX=5;
float posY=150;
int totalIceberg=0;
void setup() {
size(800, 400);
background(255);
img_boat=loadImage("imgBoat.png");
my_boat=new Boat();
my_iceberg = new Iceberg[75];
}
void draw() {
background(31, 80, 222);
my_boat.setLocation(mouseX, mouseY);
my_boat.display();
}
void mouseClicked() {
if (mousePressed==true) {
my_iceberg[totalIceberg] = new Iceberg();
totalIceberg++;
if ( totalIceberg >= my_iceberg.length)
totalIceberg = 0;
}
}
void keyPressed() {
if (keyPressed) {
if (key =='W' || key == 'w') {
posY--;
}
if (key== 'd' || key=='D') {
posX++;
}
if (key== 'a' || key =='A') {
posX--;
}
if (key== 's' || key =='S') {
posY++;
}
}
}
class Boat {
float x, y;
Boat() {
x=width/2;
y=height/2;
}
void setLocation(float tempX, float tempY) {
x=tempX;
y=tempY;
}
void display() {
image(img_boat, posX, posY);
img_boat.resize(80, 80);
}
}
class Iceberg {
float x, y; //variables for location
float speed; //variabke for speed
float c;
float r;
Iceberg() {
x=125;
y=25;
speed=random(1, 3);
r=6;
c=color(30,60,150);
img_Iceberg=loadImage("iceberg.png");
}
void setLocation(float tempoX, float tempoY) {
x=tempoX;
y=tempoY;
}
void move() {
x=x+speed;
if (x> width) x=0;
y=y-speed;
if (y<-50)y=400;
}
void display() { //displays iceberg
fill(c);
noStroke();
image(img_Iceberg,x,y);
}
}
outcome of this is its a game where you need to navigate your ship through the moving icebergs
not sure what im missing or what i could solve it with since rn my icebergs arent displaying, i had a .display function being used but the example im using doesnt use that also it doesnt use an image soo.
thanks in advance seem everyone here always seems to help
Answers
The way you have written it, right now, is that each Boat has its own image.
These images could be different! That is, each Boat could load a different image, and that image would be what that Boat looks like.
This is probably not what you want.
If all of your Boats are going to look the same, you don't need to give each Boat its own image. You'd only need one image, and every boat can use that one image.
Because you have posted some properly formatted code that demonstrates your issue, you get a gold star, and I can fix the code for you:
TF's lines 9 and 10 are important and missing in your original code. It's not enough just to define the boat, you also have to instantiate it. That's what calling
new
does.alright so i have my code as you have shown i should and no image is appearing
im not sure if im missing something or what i have it how you do
Few things. The trivial first:
setLocation()
sets x and y. However,display()
uses5,50
. Instead do this.image(boat,x,y);
Second point. I changed your code. Study this new version and reflect (or ask) what advantages you get doing it this way instead.
Kf
i do this now and still no image displays i dont know why
sooo i used a different image and it displays now
Great to hear.
Kf