As David said, you probably need to brush up on the intricacies of
Java references, object instantiation, etc. Just a few things off the
top of my head:
1. jLabel2 is an object reference and therefore can never be set to
0. It can be null, which is the state it's in when you're trying to
call setText on it. Trace your code backwards and I guarantee you
won't find a place where it's guaranteed to be set. Keep in mind that
this by itself leaves it as null until you assign it to something
else:
JLabel jLabel2;
Don't forget to keep track of objects enclosing those references. Do
you have multiple? In your example, are you instantiating jLabel2 in
one instance, but trying to use that reference belonging to another
object? Your code snippets don't make that clear.
2. Be sure you understand the difference between an object and a
reference pointing to that object. Consider the following:
Example1:
JLabel label1 = new JLabel();
JLabel label2 = label1;
label1.setText("All work and no play...");
label1 = null;
Example2:
JLabel label1 = new JLabel();
label1.setText("All work and no play...");
JLabel label2 = new JLabel(label1.getText());
label1.setText("Makes Jack a dull boy");
Play around with this and stick it in a simple GUI, reorder the lines,
add stuff. Be sure you're able to look at code like this and know
what its side effects and results are before you run it.
On Mar 31, 2:05 am, Marco <[email protected]> wrote:
> Alexey
>
> you are right, at the point i want to change the jLabel2 it is 'null',
> but it isn't 'null' when i first set it to '0'. Is this because it
> gets instantiated in an other method (gets done automatically by
> netbeans)?
>
> when i put
> jLabel2 = new JLabel();
> in front of the setText i don't get the error but the text on my
> original jLabel2 doesn't change (probably because i'm referring to the
> wrong instance.), how can i reach the correct instance or how can i
> make shure jLabel2 doesn't become 'null' ?
>
> keep in mind I am a beginner, thanks for the patience.
>
> greetings
>
> Marco
>
> On 30 mrt, 18:10, Alexey Zinger <[email protected]> wrote:
>
> > Why don't you stick a little debug output of jLabel2 value somewhere just
> > before you call jLabel2.setText.
>
> > ________________________________
> > From: Marco <[email protected]>
> > To: The Java Posse <[email protected]>
> > Sent: Tue, March 30, 2010 11:24:09 AM
> > Subject: [The Java Posse] Re: Changing JLabel text from other Class
>
> > thanks for the response
>
> > i used following code to check if i was on the EDT
>
> > if(javax.swing.SwingUtilities.isEventDispatchThread()){
> > System.out.println("is EDT");
> > System.out.println( Thread.currentThread().getName() );
> > jLabel2.setText("test");
>
> > }else{
>
> > System.out.println("is not EDT");
>
> > }
>
> > the console returns "is EDT" and the name "AWT-EventQueue-1" but still
> > gives an error on the 'jLabel.setText("test");' line
>
> > in the run method of the 'Agenda' class i use 'jLabel2.setText(""+
> > userID);' this works and gives no errors. It sets jLabel2 to the
> > original userID value of 0.
>
> > it only generates an error used in the setUserID method that gets
> > called from the 'Login' class
>
> > following lines instantiate (?) jLabel2
>
> > private javax.swing.JLabel jLabel2; (ivar)
> > jLabel2 = new javax.swing.JLabel(); (in the initComponents() method
> > that gets called in the initial run method of the 'Agenda' class)
> > the above code was generated by Netbeans.
>
> > ???
>
> > greetings
>
> > Marco
>
> > On 30 mrt, 09:17, Jan Goyvaerts <[email protected]> wrote:
>
> > > I haven't got time yet to read the whole conversation, but...
>
> > > a regular Swing beginner's error is to work outside the EDT (event
> > > dispatch
> > > thread). If you're changing the label - or anything visual for that
> > > matter -
> > > outside the EDT weird things can happen. Look into SwingUtilities to check
> > > if the current thread is the EDT. Otherwise schedule it to run in the EDT.
>
> > > MAYBE that's your problem.
>
> > > On Tue, Mar 30, 2010 at 00:43, Alexey Zinger <[email protected]>
> > > wrote:
> > > > My money is jLabel2 is null. Even if userID is null,
> > > > jLabel2.setText("" +
> > > > userID) should still work. Not to mention that calling JLabel.setText
> > > > with
> > > > null is allowed.
>
> > > > Alexey
> > > > 2001 Honda CBR600F4i (CCS)
> > > > 2002 Suzuki Bandit 1200S
> > > > 1992 Kawasaki EX500
> > > >http://azinger.blogspot.com
> > > >http://bsheet.sourceforge.net
> > > >http://wcollage.sourceforge.net
>
> > > > ------------------------------
> > > > *From:* Casper Bang <[email protected]>
> > > > *To:* The Java Posse <[email protected]>
> > > > *Sent:* Mon, March 29, 2010 6:15:55 PM
> > > > *Subject:* [The Java Posse] Re: Changing JLabel text from other Class
>
> > > > First of all, there are far better forums for these kind of questions.
> > > > A good one would be Sun's Swing forum:
> > > >http://forums.sun.com/forum.jspa?forumID=57
>
> > > > Secondly, your code is practically impossible to read because of all
> > > > the comments. When you post sample code, remember to trim it down to
> > > > just the essentials where the problem is reproducible.
>
> > > > Third, when encountering an NPE (Null Pointer Exception), one of the
> > > > referenced objects must not have been instantiated. Start tracing
> > > > backwards from there and deduce where Null could've been introduced,
> > > > either by logically tracking code paths or by using a debugger.
>
> > > > The culprit in this case is userID, based on the fact that the
> > > > exception is thrown from the event dispatching thread which is invoked
> > > > when you call setText. You have an awful lot of similar variables, uID
> > > > as the parameter, userIDapp as a copy of uID that you print out and
> > > > then finally the userID which you probably never set.
>
> > > > /Casper
>
> > > > On Mar 29, 7:34 pm, Marco <[email protected]> wrote:
> > > > > Hello,
>
> > > > > I have 2 classes one is the application and the other one the login
> > > > > screen, when i start the application i create a JFrame with the login
> > > > > screen. My login screen checks the username and password in a mysql db
> > > > > and when correct it returns the userID to the application (because the
> > > > > application needs to know this). This al works with the following
> > > > > code :
>
> > > > > in the login screen class 'Login' i have the code
> > > > > [code]
>
> > > > > Agenda ag = new Agenda();
>
> > > > > ...
>
> > > > > if(userOK){
> > > > > System.out.println("userOK gets executed."); //to check if the
> > > > > credentials where ok
> > > > > jLabel1.setBackground(Color.green);
> > > > > jLabel1.setText("You can enter"); //jLabel1 on the login
> > > > > screen itself
> > > > > System.out.printf("userid %d\n",userID); //to check the value
> > > > > of
> > > > the
> > > > > userID (this works)
> > > > > ag.setUserID(userID); //method in the application class
> > > > > 'Agenda' to set the userID there
> > > > > }
> > > > > [/code]
>
> > > > > in the application class 'Agenda' i have the setUserID method:
> > > > > [code]
> > > > > public void setUserID(int uID){
> > > > > System.out.println("setUserID"); //to check if the method gets
> > > > > called = ok
> > > > > userIDapp=uID;
> > > > > System.out.printf("userid %d\n",userIDapp);//to check if the uID
> > > > > argument gets assigned to the locale variable userIDapp = ok
> > > > > jLabel2.setText(""+ userID); //when i try to set the text of
> > > > > jLabel2 a JLabel in the 'Agenda' class i get an error also when i just
> > > > > put jLabel2.setText("test");}
>
> > > > > [/code]
>
> > > > > the error:
>
> > > > > Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
> > > > > at agenda.Agenda.setUserID(Agenda.java:93)
>
> > > > > line 93 =
> > > > > [code]
> > > > > jLabel2.setText(""+ userID);
> > > > > [/code]
>
> > > > > I'm still learning so if i make big mistakes here forgive me and teach
> > > > > me better ways
>
> > > > > thanks
>
> > > > > Marco
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > > > Groups
> > > > "The Java Posse" group.
> > > > To post to this group, send email to [email protected].
> > > > To unsubscribe from this group, send email to javaposse+
> > > > [email protected].
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/javaposse?hl=en.
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > > > Groups
> > > > "The Java Posse" group.
> > > > To post to this group, send email to [email protected].
> > > > To unsubscribe from this group, send email to
> > > > [email protected]<javaposse%2bunsubscr...@googlegroups
> > > > .com>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/javaposse?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "The Java Posse" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group
> > athttp://groups.google.com/group/javaposse?hl=en.
--
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.