简体   繁体   中英

There are errors but I can't see where

For some reason I am receiving errors in the code below and I can't see why, can you spot any?

public void delTask_mouseClicked(MouseEvent e)
{
    if(delTask.isEnabled() == false) {
        int numTasks = taskTable.getRowCount();
        Object[] currentTasks;
        currentTasks = new Object[numTasks];
        for (int i = 0; i < numTasks ; i++){
            Object tasks = taskTable.getModel().getValueAt(i, 1);
            currentTasks[i] = tasks;
        }
        System.out.println(currentTasks);
     }
}

Thanks for the help, it's really appreciated.

There massive block of errors I am getting is below:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at projecttaskmanagement.ProjectGUI.delTask_mouseClicked(ProjectGUI.java:233)
at projecttaskmanagement.ProjectGUI$2.mouseClicked(ProjectGUI.java:109)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:212)
at java.awt.Component.processMouseEvent(Component.java:5520)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
at java.awt.Component.processEvent(Component.java:5282)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3984)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3901)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

CODE AT LINE 233: int numTasks = taskTable.getRowCount();

TASK TABLE IS DEFINED BELOW:

    String[] taskcolumnNames = {"ID #",
            "Name",
            "Description",
            "Start Date",
            "End Date",
            "Staff",
            "Completed"};
    Object[][] taskdata = {
            {new Integer(1), "Requirements Analysis",
             "Analysing the requirements",
             "01/09/2011", "15/10/2011",
             "Bob", new Boolean(true)},
            {new Integer(2), "System Design",
             "Designing the System",
             "15/09/2011", "15/10/2011",
             "Alice", new Boolean(true)},
            {new Integer(3), "Code (A)",
             "Part 'A' of coding",
             "01/10/2011", "15/11/2011",
             "David", new Boolean(true)},
        };
    JTable taskTable = new JTable(taskdata, taskcolumnNames);

While we're waiting for you to post the actual errors you're getting (a) , please take a moment to NEVER do this:

if (delTask.isEnabled() == false)

A much better form is the simpler-to-read:

if (! delTask.isEnabled())

We now return you to your scheduled programming, pending your update.


Dum de dum de dum ...

Now, based on your update, the following part of the stackdump:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at projecttaskmanagement.ProjectGUI.delTask_mouseClicked(ProjectGUI.java:233)

indicates where the problem lies. Find out which of those lines of yours in line number 233 and there you have it. The thing that you're trying to deference on that line is in fact a null reference.

Based on the snippet, it'll probably be one of the following lines:

public void delTask_mouseClicked(MouseEvent e)
{
    if(delTask.isEnabled() == false) {                            // <--
        int numTasks = taskTable.getRowCount();                   // <--
        Object[] currentTasks;
        currentTasks = new Object[numTasks];
        for (int i = 0; i < numTasks ; i++){
            Object tasks = taskTable.getModel().getValueAt(i, 1); // <--
            currentTasks[i] = tasks;
        }
        System.out.println(currentTasks);
     }
}

The first will be because delTask itself is null, the second if taskTable is null.

The third will be if taskTable itself is valid but the value returned from its getModel() method is null.


So, it appears that your taskTable is null. As to why this is so, that's unknowable based on the current information. What you will need to do is examine all the places it's set to a valid value and ensure that this happens before you (or more likely, AWT under the control of your user) call this method.

And of course, make sure it's not set back to NULL at some point after creation.

If you can't guarantee that, you'll probably need to change:

if (delTask.isEnabled() == false)

into something like:

if ((! delTask.isEnabled()) && (taskTable != NULL))

but my preference would be to fix the root cause of the problem rather than applying this band-aid.

Your code that creates the JTable seems okay (syntactically) but there's the slight mystery of where that's done. Is it created in a manner that it's usable from where you're trying to use it.

For example, if that code that creates it is within the constructor, that particular taskTable would be local to said constructor (and destroyed on exit), not usable from elsewhere. In that case, it needs to be made an object-level variable so that other methods can get to it.

You can see that effect in the following program:

public class testprog {
    public Object thingOne;
    public Object thingTwo;

    public void someFunction() {
        thingOne = new Object();
        Object thingTwo = new Object();
    }

    public void debug() {
        if (thingOne == null)
            System.out.println ("thingOne is NULL");
        else
            System.out.println ("thingOne is valid");
        if (thingTwo == null)
            System.out.println ("thingTwo is NULL");
        else
            System.out.println ("thingTwo is valid");
    }

    public static void main(String args[]) {
        testprog tp = new testprog();
        tp.someFunction();
        tp.debug();
    }
}

This outputs:

thingOne is valid
thingTwo is NULL

because thje thingTwo set up in someFunction() is a local version and does not in any way set up the object-level thingTwo - the object level one remains as null and, if you try to dereference it, you'll see the same problem you're having.


(a) The best problems reports come with a small, complete code snippet exhibiting the problem, the expected behaviour, and the actual behaviour.

If we post that sample of yours into a naked Eclipse Java program, it's very much not complete. MouseEvent , delTask and taskTable have no definitions and, without that information, it's a little hard to debug.

In addition, Eclipse (for syntax errors) and Java itself (for runtime errors) are perfectly able to tell you in great detail what your problems are, and you should read what it's telling you. You should also communicate that information to us if you want help :-)

Which line is line 233 of ProjectGUI.java ? At least one of the following is null:

  • delTask
  • taskTable
  • taskTable.getModel()

Figure out which of those falls on line 233 (per your error report), and you've figured out where the problem lies. We'll need to see more code to determine why the variable does not have the expected value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM