HELP! Java Programming.....

  • Thread starter Deleted User
  • Start date
  • Views 1,349
  • Replies 8
D

Deleted User

Guest
OP
Hi all, hoping someone can help a java noob here.

Learning Java in my college course, but I've come across a little snag.

I have to create a little app to draw some orbs on a display, but I always get "NullPointerException" error when I enter a value.

I understand that it's because I'm not initialising my target integer, but I cannot for the love of me understand WHY I'm not initialising it.

Anyone wanna point out to me what I'm doing and why?

Main

import javax.swing.*;

Action ac;

void setup(){
size(1920,1080);
noStroke();
frameRate(144);
fill(255); //background white (for now)
rect(0,0,1920,1080); //Right screen edge block
fill(0,120,200); //Edge data block colors, light blue
rect(0,0,550,1080); //Left data block
rect(1370,0,1920,1080); //Right data block
textSize(60);
text("BlitzBall v0.1", 95, 60);
int flotsome = Integer.parseInt(JOptionPane.showInputDialog("How many balls to spawn ", "2"));
ac.setAmount(flotsome);
}

void draw(){
ac.display();
print(ac.amount);
}

Action class
class Action{
private int amount;
int xLoc[];
int yLoc[];
//============
//Constructors
//============
Action(){
}

Action(int amount){
this.amount = amount;
print("Constructor check " + amount);
}

Action(int xLoc, int yLoc){
//Dummy for now
}

//=======
//Methods
//=======
void display(){
if(amount == 1){
ellipse(900,540,50,50);
}
else if(amount == 2){
ellipse(800,540,50,50);
ellipse(900,540,50,50);
}
else if(amount == 3){
ellipse(800,540,50,50);
ellipse(900,540,50,50);
ellipse(1000,540,50,50);
}
else{
}
}

//=======
//Setters
//=======
public void setAmount(int amount){
if((amount > 0) && (amount <= 3)){
this.amount = amount;
print("Setter Check " +amount);
}
}

/*
public void setLocs(int passX, int passY){
if((passX >= 0) && (passX <= 1920)){
//this.xLoc = passX;
}
else{
}
if((passY >= 0) && (passY <= 1080)){
//this.yLoc = passY;
}
else{
}
}
*/

//=======
//Getters
//=======
}
 
Last edited by ,

KleinesSinchen

GBAtemp's Backup Reminder + Fearless Testing Sina
Member
GBAtemp Patron
Joined
Mar 28, 2018
Messages
4,455
Trophies
2
XP
14,943
Country
Germany
That got it! Now to try and understand WHY that worked.

Thanks for the help :)

Your class is named "Action"
You want a new Object. Object type: "Action" Name of the Object/ the variable: ac

Action ac;

Java uses references (similar to pointers). Where does it point now? Nowhere. We don't have any Object of the type "Action" yet. So we create a new one.
ac = new Action(); // make new object of type "Action" and use the constructor without arguments
All objects must be created. You could as well do this with any primitive datatype (although it makes no sense)
int i = new Integer(9); // int i=9;
 
D

Deleted User

Guest
OP
Your class is named "Action"
You want a new Object. Object type: "Action" Name of the Object/ the variable: ac

Action ac;

Java uses references (similar to pointers). Where does it point now? Nowhere. We don't have any Object of the type "Action" yet. So we create a new one.
ac = new Action(); // make new object of type "Action" and use the constructor without arguments
All objects must be created. You could as well do this with any primitive datatype (although it makes no sense)
int i = new Integer(9); // int i=9;
Ahhhhh that explains it nice and clearly. Thanks mate, appreciate it, was tearing my hair out trying to figure it out for the past 2 days now.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    Veho @ Veho: Her boob is the cause of all this current shit :angry: