Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
so here is my code: Code: public static void main(String args[]) { String theinfo; addmfr_resell mfr = new addmfr_resell(); mfr.addMfrSell ("Hasbro"); mfr.addMfrSell ("Dewall Power Tools"); mfr.addMfrSell ("Nintendo"); mfr.addMfrSell("Apple"); mfr.addMfrSell ("Ajax"); ...
  1. #1
    Linux Newbie dalinux_n00bie's Avatar
    Join Date
    Nov 2006
    Posts
    226

    Java NullPointerException help

    so here is my code:
    Code:
     public static void main(String args[])
        {
            String theinfo;
            addmfr_resell mfr = new addmfr_resell();
            mfr.addMfrSell ("Hasbro");
            mfr.addMfrSell ("Dewall Power Tools");
            mfr.addMfrSell ("Nintendo");
            mfr.addMfrSell("Apple");
            mfr.addMfrSell ("Ajax");
            mfr.addMfrSell ("Acme");
            mfr.addMfrSell ("Napa");
            mfr.addMfrSell ("Eureka");
            mfr.addMfrSell ("Hoover");
            theinfo = mfr.toString();
            showinfo.setText (theinfo);
            
            java.awt.EventQueue.invokeLater(new Runnable() 
            {
                public void run() 
                {
                    new retailGUI().setVisible(true);
                  
                }
            });
           
        }
    I keep getting a NullPointerException at "showinfo.setText (theinfo);"
    I am at a complete lost as to how to fix this... I swear that everything is declared... whats going on?
    "Do or do not...there is no try" -Yoda
    History is a set of lies agreed upon by the winners.
    Linux is user friendly, not idiot friendly.
    Linux User 437442

  2. #2
    Just Joined! aluminumspleen's Avatar
    Join Date
    Oct 2008
    Location
    Dorm Room, America
    Posts
    59
    is showinfo declared? cause if not, or if it's null, it will throw a NullPointerException

  3. #3
    Linux Newbie dalinux_n00bie's Avatar
    Join Date
    Nov 2006
    Posts
    226
    I'm pretty sure it is declared.... showinfo is a JTextArea, what whould i have to do to declare it? I build the GUI before i start that snippit of code...
    "Do or do not...there is no try" -Yoda
    History is a set of lies agreed upon by the winners.
    Linux is user friendly, not idiot friendly.
    Linux User 437442

  4. #4
    Just Joined! aluminumspleen's Avatar
    Join Date
    Oct 2008
    Location
    Dorm Room, America
    Posts
    59
    did you construct it ever? if it was a data member, and was never constructed, it will just be a null object, and you can't set the text of a null object.

  5. #5
    Linux Newbie dalinux_n00bie's Avatar
    Join Date
    Nov 2006
    Posts
    226
    i thought i did... how could i tell if i constructed it?
    "Do or do not...there is no try" -Yoda
    History is a set of lies agreed upon by the winners.
    Linux is user friendly, not idiot friendly.
    Linux User 437442

  6. #6
    Just Joined! aluminumspleen's Avatar
    Join Date
    Oct 2008
    Location
    Dorm Room, America
    Posts
    59
    JTextArea showinfo = new JTextArea();

    this is the default constructur, with x and y set to zero, and the string text set to null. you can check out the other constructors on the API under "Constructor Summary":

    JTextArea (Java Platform SE 6)

  7. #7
    Linux Newbie dalinux_n00bie's Avatar
    Join Date
    Nov 2006
    Posts
    226
    showinfo = new javax.swing.JTextArea();

    Isn't that the same thing?
    "Do or do not...there is no try" -Yoda
    History is a set of lies agreed upon by the winners.
    Linux is user friendly, not idiot friendly.
    Linux User 437442

  8. #8
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    Have you tried stepping through it using a debugger? Put a breakpoint on the line where you're attempting to set the value and check all the existing variables to see which one is null.
    Registered Linux user #270181
    TechieMoe's Tech Rants

  9. #9
    Linux Newbie dalinux_n00bie's Avatar
    Join Date
    Nov 2006
    Posts
    226
    I want to make sure i'm understanding this...

    I get the nullpointerexception because my object showinfo isn't constructed right? but does it not get constructed when i tell it to show up for my GUI?
    "Do or do not...there is no try" -Yoda
    History is a set of lies agreed upon by the winners.
    Linux is user friendly, not idiot friendly.
    Linux User 437442

  10. #10
    Linux Newbie dalinux_n00bie's Avatar
    Join Date
    Nov 2006
    Posts
    226
    Quote Originally Posted by techieMoe View Post
    Have you tried stepping through it using a debugger? Put a breakpoint on the line where you're attempting to set the value and check all the existing variables to see which one is null.
    I did that, and everything went as it was supposed to, but it still gave me my null exception...

    ok, so correct me if i'm wrong, but the setText() allows me to set the text of an object right? So I have my JTextArea and i want to set the text in there to strings in my array list right?
    "Do or do not...there is no try" -Yoda
    History is a set of lies agreed upon by the winners.
    Linux is user friendly, not idiot friendly.
    Linux User 437442

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...