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%[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]. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.
