Hi,
I get this error:
Exception in thread "Thread-4" java.lang.NullPointerException

When I try to send the vector to my form.
Does someone knows where I made a mistake?

Here is a part of my code:
(this part is from my clientapplication (where I make a connection with the server)

if (msg.equals("Schools")) {
schools= (Vector<school>) istream.readObject();
System.out.println("amount of schools: " + schools.size());
System.out.println(schools);
till here everything worked fine. when I print the vector schools I get all the items in this vector.


frmschool.showSchools(schools);

Here I got the nullpointer exception.
here the part where I want to place the schools in a textbox:
(frmschool):

public void showUsers(Vector<School> schools) {
txtOnline.setText("");
for (School s : schools) {
txtOnline.append(s.toString() + "\n");
//        }


}

-----------------------------------------------------------------
Thanks in advance!

Dani AI

Generated

A NullPointerException here usually means one of three things: the form reference you call is null, the form’s text component (txtOnline) was never created, or the call is happening at the wrong time/thread. flagged the method-name confusion and correctly pointed toward a null UI field — use the stack trace line number to tell whether the NPE is thrown at the call site (frmschool.showSchools(...)) or inside the form method (accessing txtOnline).

Quick checks to run now:

  • Print the form reference right before the call to confirm it isn’t null.
  • Inside the form’s showSchools method, print (or breakpoint) the txtOnline reference first to see if it’s null.
  • Verify the form instance you’re calling is the same instance that was shown on screen (no accidental second instance).

Make UI updates on the Event Dispatch Thread. From the background/network thread, schedule the update like this:

SwingUtilities.invokeLater(new Runnable() {
  public void run() {
    myForm.showSchools(schools);
  }
});

And inside the form, guard against an uninitialized text area and update safely:

public void showSchools(final Vector<School> schools) {
  SwingUtilities.invokeLater(() -> {
    if (txtOnline == null) {
      System.err.println("txtOnline is null — initComponents() may not have run or wrong instance used");
      return;
    }
    txtOnline.setText("");
    for (School s : schools) {
      txtOnline.append(s.toString() + "\n");
    }
  });
}

Common root causes and fixes:

  • Calling showSchools on a null frmschool variable — ensure the instance was constructed and stored in the field you use.
  • Using a different FrmSchool instance than the one shown — pass the displayed instance to your client logic.
  • Relying on UI components before initComponents() or before setVisible(true) completes — construct the GUI first.
  • Updating Swing components from a non-EDT thread — wrap updates with SwingUtilities.invokeLater or use a SwingWorker.

For more on Swing threading, see the Oracle Swing concurrency tutorial: https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html and the SwingUtilities API: https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingUtilities.html.

Recommended Answers

All 5 Replies

showSchools or showUsers???

I think txtOnline should be null..... print txtOnline just inside the showSchools method

it's showschools, (sorry I copied the wrong method, but the inside is the same)

it's showschools, (sorry I copied the wrong method, but the inside is the same)

txtOnline should be null just inside ur method... so any operation on txtOnline will throw null pointer

txtOnline should be null just inside ur method... so any operation on txtOnline will throw null pointer

Thanks, but how do I do this?

When I have this method:

public void showSchools(Vector<Object> schools) {
         txtOnline.setText("");
        for (Object s : schools) {
            txtOnline.append(s.toString() + "\n");
            System.out.println(schools.toString());  --> here I get the values of the vector, but still not in my txtOnline. 
        }
        System.out.println(schools.toString());
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.