HELP! Java Programming.....

  • Thread starter Deleted User
  • Start date
  • Views 1,351
  • 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,469
Trophies
2
XP
15,020
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:
    Double dickhead chinballs is still better than double dickhead eyeballs.
    +1
  • Veho @ Veho:
    As in, the balls will grow in your eye sockets.
  • K3Nv2 @ K3Nv2:
    I paid 5 grand to get them moved to my chin
    +1
  • Veho @ Veho:
    This you?
  • K3Nv2 @ K3Nv2:
    My hair can't be that cool
    +1
  • Veho @ Veho:
    Ah, yes, portrait mode, surely the best way to film a row of people. If only there were some way to fit a wider shot, at the expense of height... if only...
    +1
  • K3Nv2 @ K3Nv2:
    4k portrait mode?
    +1
  • BigOnYa @ BigOnYa:
    Diddy - "I never touched her, that bitch is crazy." Video is released. Diddy - " Ok I did it, i am remorseful for my actions during my darkest times." Lol
  • SylverReZ @ SylverReZ:
    @BigOnYa, Glad that the Diddler got caught once again.
    +1
  • K3Nv2 @ K3Nv2:
    Iran ran out of options
  • K3Nv2 @ K3Nv2:
    Thought I saw my ex on that new kingdom of the Apes poster
  • BigOnYa @ BigOnYa:
    I thought I saw a puttie snatch...
  • BigOnYa @ BigOnYa:
    I'm so pumped, NCAA football video game is back after a 10 year hiatus, coming to SeriesS/X, PS5 in July.
  • BigOnYa @ BigOnYa:
    Wish they would let us play the NCAA football 14 on newer xbox, its one of the only games I love and own on disc still, that is not back compatible, bs.
  • AdenTheThird @ AdenTheThird:
    @BigOnYa My dad recently bought a spare Series X off of me, got game pass, and saw Madden '24 on Game Pass, decided to try it out.

    ...He was a bit taken aback by the 60GB download size. Poor guy's still living in the 90s!
    +1
  • SylverReZ @ SylverReZ:
    @AdenTheThird, Bro's still in the PS2 age.
  • K3Nv2 @ K3Nv2:
    Lol charging your dad's classic unless he's the type that doesn't like taking things like that
  • AdenTheThird @ AdenTheThird:
    @K3Nv2 He was looking into consoles for his house anyway (for my younger siblings and himself) and I had a spare XSX and Switch I ended up selling him at pretty steep cuts. I would just give them to him, but I did buy them with the intent to sell them... and college is super expensive.
    AdenTheThird @ AdenTheThird: @K3Nv2 He was looking into consoles for his house anyway (for my younger siblings and himself)...