3

I have a client server application. Messages are being processed back and forth in the application.

I suspect that something doesn't have enough time to respond and/or it's not created in time.

This specific area of the code is processing a message where it sets data in the client. The way messages are processed is via commands. These commands are put into a thread pool executor. I get the below exception during runtime. However, when I debug and step into the execute method and then step over each line, I never hit the exception. This makes it really difficult to debug.

Any debugging suggestions would be highly appreciated as I am new to thread management( hopefully it has nothing to do with threads ).

Exception in thread "pool-1-thread-1" java.lang.NullPointerException
at Client.Commands.DisplayProjects.execute(DisplayProjects.java:47)
at Client.Commands.ClientCommand.run(ClientCommand.java:31)
at Client.Commands.ProcessData.execute(ProcessData.java:24)
at Client.Commands.ClientCommand.run(ClientCommand.java:31)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

Below is the execute method:

    @Override
public void execute(Client client) {
            /* Create and display the form */
    java.awt.EventQueue.invokeLater(() -> {
        new SplashScreen(client).setVisible(true);
    });
    System.out.println("SplashScreen is null"+client.splashscreen==null);
    client.splashscreen.setProjects(projects);
}

Below is the setProjects method:

    public void setProjects(ArrayList<String> projects){
    DefaultTableModel model = new DefaultTableModel();
    int row = 0;model.setRowCount(0);
    model.addColumn("Application", new Object[0]);
    model.addColumn("Description", new Object[0]);
    Iterator<String> iterator = projects.iterator();
    while(iterator.hasNext()){
        String name = iterator.next();
        String[] projectRow = new String[2];
        projectRow[0] = iterator.next();projectRow[1]="";
        model.addRow(projectRow);
    }
    ProjectTable.setModel(model);

}
Mark
  • 911
  • 13
  • 30
  • 2
    Please visit the [help] and read [ask]. You MUST show us the source code or the question will be rapidly closed as off-topic. Also, have you read http://stackoverflow.com/q/218384/18157 – Jim Garrison Jun 26 '16 at 19:13
  • 7
    Because you have race conditions. Yes, they are annoying to debug, but that is something you have to deal with now that you have gotten yourself into that situation. Try adding System.out.println-Statements to see what your variables are and hope that that additional time does not remove the NPE as well. – luk2302 Jun 26 '16 at 19:13
  • @Jim - I added the affected code. I am aware of standard debugging. I have a specific problem where I cannot reproduce the null pointer in debug mode. – Mark Jun 26 '16 at 19:18
  • @luk - I was hoping there was a better way :) . I was going to do that as a last resort. Luckily the affected code is not that many lines so it will not be too bad. – Mark Jun 26 '16 at 19:19
  • @luk - Actually, that is the answer to my question. It is called a "Race Condition". That really helps me move forward knowing what is happening. Now that I think of it I have heard of that before it just didn't come to mind. It 's a huge help knowing what terminology to research. Thanks again!!! – Mark Jun 26 '16 at 19:29
  • I found the race condition. When I launch the splashscreen I am using "invokeLater" and I need to use "invokeAndWait". Otherwise it's null by the time the other thread gets to the point that it is needed. – Mark Jun 26 '16 at 19:53

0 Answers0