Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Null Pointer Exception?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Null Pointer Exception?

    I am getting a null pointer exception and can not figure out what I'm missing here. Does anyone have any idea what I'm missing?

    import javax.swing.*;

    public class Truck{

    public int [] choice;
    public String [] model;
    public int [] cd;
    public int [] speaker;
    public int [] cost;

    public int choiceCount;
    public int modelCount;
    public int cdCount;
    public int speakerCount;
    public int costCount;

    public String summaryOutput;

    public void Truck(){

    choice = new int[100];
    model = new String[100];
    cd = new int[100];
    speaker = new int[100];
    cost = new int[100];

    choiceCount = 0;
    modelCount = 0;
    cdCount = 0;
    speakerCount = 0;
    costCount = 0;

    summaryOutput = "";

    }//end Truck method

    public String WithSpeakerSummary(){

    for(int i = 0; i < choiceCount; i++){

    if(choice[i] == 1){

    summaryOutput += "Cars with Stereos:/nModel: " + model[i] +
    "/nCost: " + cost[i] +
    "/n# CDs: " + cd[i] +
    "/n# Speakers: " + speaker[i] +
    "/n/n";

    }//end if statement

    }//end


    return summaryOutput;
    }//end Summary method

    public String WithOutSpeakerSummary(){

    for(int i = 0; i < choiceCount; i++){

    if(choice[i] == 0){

    summaryOutput += "Cars with Stereos:/nModel: " + model[i] +
    "/nCost: " + cost[i] +
    "/n/n";

    }//end if statement

    }//end


    return summaryOutput;
    }//end Summary method

    public void addWithChoice(int w){

    choice[choiceCount++] = w;

    }//end addWithChoice method

    public void addWithoutChoice(int wo){

    choice[choiceCount++] = wo;

    }//end addWithoutChoice method

    public void addModel(String m){

    model[modelCount++] = m;

    }//end addModel method

    public void addCD(int c){

    cd[cdCount++] = c;

    }//end addModel method

    public void addSpeaker(int s){

    speaker[speakerCount++] = s;

    }//end addModel method

    public void addCost(int t){

    cost[costCount++] = t;

    }//end addModel method

    }//end Truck super class

    import javax.swing.*;

    public class TruckWithStereo extends Truck{

    public static void main(String[] args){


    int aChoice = 0;
    String aModel = "";
    int aCD = 0;
    int aSpeaker = 0;
    int aCost = 0;

    Truck tObject = new Truck();

    do{

    while(JOptionPane.showConfirmDialog(null, "Does this car have a stereo?") == JOptionPane.YES_OPTION){

    //with speakers
    aChoice = 1;
    tObject.addWithChoice(aChoice);

    aModel = JOptionPane.showInputDialog("Model of car?");
    tObject.addModel(aModel);

    aCD = Integer.parseInt(JOptionPane.showInputDialog("How many CDs in the player?"));
    tObject.addCD(aCD);

    aSpeaker = Integer.parseInt(JOptionPane.showInputDialog("How many speakers in the car?"));
    tObject.addSpeaker(aSpeaker);

    aCost = Integer.parseInt(JOptionPane.showInputDialog("Cost of car?"));
    tObject.addCost(aCost);

    }//end YES Option while loop


    while(JOptionPane.showConfirmDialog(null, "Does this car have a stereo?") == JOptionPane.NO_OPTION){

    //without speakers
    aChoice = 0;
    tObject.addWithoutChoice(aChoice);

    aModel = JOptionPane.showInputDialog("Model of car?");
    tObject.addModel(aModel);

    aCost = Integer.parseInt(JOptionPane.showInputDialog("Cost of car?"));
    tObject.addCost(aCost);

    }//end NO Option while loop

    }while(JOptionPane.showConfirmDialog(null, "Still looking for cars?") == JOptionPane.YES_OPTION);//end do-while loop



    if(JOptionPane.showConfirmDialog(null, "Do you want to see cars with stereos?") == JOptionPane.YES_OPTION){

    tObject.WithSpeakerSummary();

    }//end YES Option while loop


    else if(JOptionPane.showConfirmDialog(null, "Do you want to see cars with stereos?") == JOptionPane.NO_OPTION){

    tObject.WithOutSpeakerSummary();

    }//end NO Option while loop


    }//end main method



    }//end TruckWithStereo subclass


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Null Pointer Exception?

    The error message will tell on which line it occurs. Go to that line and examine all your variables to determine which is null. Then make sure it is not null before you try to use it.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer Exception?

    I did not realize that. I checked the lines, but still can not figure out what the problem is. I have initialized the variables.

    The first is on line 29 in the TruckWithStereo class

    tObject.addWithChoice(aChoice);
    The second is on line 85 in the Truck class

    choice[choiceCount++] = w;

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Null Pointer Exception?

    Copy and paste the exact error message.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer Exception?

    Oh sorry here is the entire thing. If you are wondering about the IT206.labbassignments.lab2assignment. that's the package that I have the two files in.

    Exception in thread "main" java.lang.NullPointerException
    at IT206.labassignments.lab2assignment.Truck.addWithC hoice(Truck.java:85)
    at IT206.labassignments.lab2assignment.TruckWithStere o.main(TruckWithStereo.java:29)
    Java Result: 1

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Null Pointer Exception?

    The error is on line 85 of the Truck class which according to you is: choice[choiceCount++] = w;
    That means that the choice variable is null and has not been instantiated. "But it has been in the Truck constructor" I here you cry. Well you might want to check that. Hint: methods and constructors are different things.
    Improving the world one idiot at a time!

  7. The Following User Says Thank You to Junky For This Useful Post:

    Mr.777 (November 16th, 2011)

  8. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Uhhh do you think you could give a better hint then that? haha

    I apologize this has been driving me nuts and I am resorting to the forum because I'm hopelessly lost now.

  9. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Null Pointer Exception?

    I almost pointed you directly to the problem. Where is the constructor in the Truck class? It doesn't have one.
    Improving the world one idiot at a time!

  10. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    So the Truck method I created is not serving as a Truck constructor in that class then?

  11. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Null Pointer Exception?

    Of course not since methods are method not constructors. If you do not understand the difference then you need to do some revision.
    Improving the world one idiot at a time!

  12. #11
    Junior Member
    Join Date
    Sep 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    After reading your last post I did do some reviewing and realized what I did to cause those errors. thanks for the help! turns out there were more that arose but I got all the bugs out finally.

Similar Threads

  1. Null Pointer exception
    By Demetrius82 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 2nd, 2011, 07:32 PM
  2. Null Pointer Exception Help !!
    By AlterEgo1234 in forum Member Introductions
    Replies: 1
    Last Post: March 27th, 2011, 10:07 AM
  3. Null Pointer Exception Help!!
    By puzzledstudent in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 11th, 2010, 06:46 PM
  4. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM
  5. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM