Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有错误,但我可以';我不知道在哪里_Java_Eclipse_User Interface - Fatal编程技术网

Java 有错误,但我可以';我不知道在哪里

Java 有错误,但我可以';我不知道在哪里,java,eclipse,user-interface,Java,Eclipse,User Interface,由于某种原因,我在下面的代码中收到错误,我看不出原因,你能发现任何错误吗 public void delTask_mouseClicked(MouseEvent e) { if(delTask.isEnabled() == false) { int numTasks = taskTable.getRowCount(); Object[] currentTasks; currentTasks = new Object[numTasks];

由于某种原因,我在下面的代码中收到错误,我看不出原因,你能发现任何错误吗

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);
     }
}
第233行代码: int numTasks=taskTable.getRowCount()

任务表定义如下:

    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);

在我们等待您发布您收到的实际错误(a)时,请花点时间不要这样做:

更好的形式是更简单的阅读:

if (! delTask.isEnabled())
我们现在将您返回到计划的编程,等待您的更新



现在,根据您的更新,stackdump的以下部分:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at projecttaskmanagement.ProjectGUI.delTask_mouseClicked(ProjectGUI.java:233)
指示问题所在。在第233行中找出你的哪一行,你就知道了。你试图在那一行上尊重的东西实际上是一个空引用

根据代码段,它可能是以下行之一:

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);
     }
}
变成类似于:

if ((! delTask.isEnabled()) && (taskTable != NULL))
<>但是我喜欢解决问题的根本原因,而不是应用这个创可贴。

您创建
JTable
的代码(从语法上)似乎还可以,但在哪里创建JTable有点神秘。它是以一种可以从您尝试使用它的地方使用的方式创建的

例如,如果创建它的代码在构造函数中,那么特定的
taskTable
将是所述构造函数的本地(并在退出时销毁),不能从其他地方使用。在这种情况下,需要将其设置为对象级变量,以便其他方法可以访问它

您可以在以下程序中看到这种效果:

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();
    }
}
这将产生:

thingOne is valid
thingTwo is NULL
因为在
someFunction()
中设置的
thingTwo
是本地版本,并且没有以任何方式设置对象级别
thingTwo
-对象级别1保持为
null
,如果尝试取消引用它,您将看到与您遇到的问题相同的问题


(a) 最好的问题报告附带一个小而完整的代码片段,展示问题、预期行为和实际行为

如果我们将您的示例发布到一个赤裸裸的Eclipse Java程序中,那么它是非常不完整的
MouseEvent
delTask
taskTable
没有定义,没有这些信息,调试有点困难


此外,Eclipse(用于语法错误)和Java本身(用于运行时错误)完全能够非常详细地告诉您问题是什么,您应该阅读它告诉您的内容。如果您需要帮助,还应将该信息告知我们:-)

在我们等待您发布您收到的实际错误时(a),请花点时间不要这样做:

更好的形式是更简单的阅读:

if (! delTask.isEnabled())
我们现在将您返回到计划的编程,等待您的更新



现在,根据您的更新,stackdump的以下部分:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at projecttaskmanagement.ProjectGUI.delTask_mouseClicked(ProjectGUI.java:233)
指示问题所在。在第233行中找出你的哪一行,你就知道了。你试图在那一行上尊重的东西实际上是一个空引用

根据代码段,它可能是以下行之一:

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);
     }
}
变成类似于:

if ((! delTask.isEnabled()) && (taskTable != NULL))
<>但是我喜欢解决问题的根本原因,而不是应用这个创可贴。

您创建
JTable
的代码(从语法上)似乎还可以,但在哪里创建JTable有点神秘。它是以一种可以从您尝试使用它的地方使用的方式创建的

例如,如果创建它的代码在构造函数中,那么特定的
taskTable
将是所述构造函数的本地(并在退出时销毁),不能从其他地方使用。在这种情况下,需要将其设置为对象级变量,以便其他方法可以访问它

您可以在以下程序中看到这种效果:

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();
    }
}
这将产生:

thingOne is valid
thingTwo is NULL
因为在
someFunction()
中设置的
thingTwo
是本地版本,并且没有以任何方式设置对象级别
thingTwo
-对象级别1保持为
null
,如果尝试取消引用它,您将看到与您遇到的问题相同的问题


(a) 最好的问题报告附带一个小而完整的代码片段,展示问题、预期行为和实际行为

如果我们将您的示例发布到一个赤裸裸的Eclipse Java程序中,那么它是非常不完整的
MouseEvent
delTask
taskTable
没有定义,没有这些信息,调试有点困难


此外,Eclipse(用于语法错误)和Java本身(用于运行时错误)完全能够非常详细地告诉您问题是什么,您应该阅读它告诉您的内容。如果您需要帮助,还应将该信息告知我们:-)

哪一行是
ProjectGUI.java
的第233行?以下项中至少有一项为空:

  • delTask
  • taskTable
  • taskTable.getModel()

找出其中哪一个落在第233行(根据您的错误报告),您就找到了问题所在。我们需要查看更多代码来确定变量没有预期值的原因。

哪一行是
ProjectGUI.java
的第233行?以下项中至少有一项为空:

  • delTask
  • taskTable
  • taskTable.getModel()

找出其中哪一个落在第233行(根据您的错误报告),您就找到了问题所在。我们需要查看更多代码来确定变量没有预期值的原因。

第233行:int numTasks=taskTable.getRowCount();这表示
taskTable
为空。它试图对不存在的对象调用
getRowCount()
函数。
taskTable
def的位置和方式