简体   繁体   English

程序中的空指针异常

[英]Null Pointer Exception in program

The Below code does not fail to compile but however at runtime it says java.lang.NullPointerException at Line at line number 20 and 41. Also i am little bit curious to know what is Null Pointer Exception, what happens at runtime ? 下面的代码不会失败,但是在运行时会在第20行和第41行的行中显示java.lang.NullPointerException。同样,我有点好奇地知道什么是Null Pointer Exception,在运行时会发生什么?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Tool 
    {
     private JToolBar toolbar1;
     private JToolBar toolbar2;
     private JPanel panel;
     public Tool()
        {
         JFrame frame= new JFrame();
         panel = new JPanel();
         panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
         JButton one = new JButton("one");
         JButton two = new JButton("two");
         JButton three = new JButton("three");
         JButton four = new JButton("four");
             toolbar1 = new JToolBar();
             toolbar2 = new JToolBar();
         toolbar1.add(one);
         toolbar1.add(two);
         toolbar2.add(three);
         toolbar2.add(four);
         toolbar1.setAlignmentX(0);
         toolbar2.setAlignmentX(0);
         panel.add(toolbar1);
         panel.add(toolbar2);
         frame.add(panel,BorderLayout.NORTH);

         frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
         frame.setSize(400,300);
         frame.setTitle("ZOOP");
         frame.setVisible(true);
        }




        public static void main (String args[])
        {
         Tool zoop = new Tool();
        }


    }

You are passing a null on the following methods.... 您正在通过以下方法传递null ...

     panel.add(toolbar1);
     panel.add(toolbar2);

It's because the following haven't been initialized. 这是因为以下内容尚未初始化。

 private JToolBar toolbar1;
 private JToolBar toolbar2;

Definition of NullPointerException NullPointerException的定义

Thrown when an application attempts to use null in a case where an object is required. 当应用程序在需要对象的情况下尝试使用null时抛出。 These include: 这些包括:

  • Calling the instance method of a null object. 调用空对象的实例方法。
  • Accessing or modifying the field of a null object. 访问或修改空对象的字段。
  • Taking the length of null as if it were an array. 将null的长度视为数组。
  • Accessing or modifying the slots of null as if it were an array. 访问或修改null插槽,就好像它是一个数组一样。
  • Throwing null as if it were a Throwable value. 将null抛出,就好像它是一个Throwable值一样。

Initialize it 初始化

 JToolBar toolbar1 = new JToolBar(SwingConstants.HORIZONTAL);
 JToolBar toolbar2 = new JToolBar(SwingConstants.VERTICAL);

You haven't actually allocated toolbar1 or toolbar2 . 您实际上尚未分配工具 toolbar1或工具toolbar2 You need to do something like: 您需要执行以下操作:

toolbar1 = new JToolBar ();
toolbar2 = new JToolBar ("other toolbar");

just like you did with: 就像您使用:

JButton one = new JButton("one");

The reason you're getting the exception is because you're trying to dereference it and there's nothing there. 收到异常的原因是因为您试图取消引用它,所以那里什么也没有。

See here for the JToolBar docs. 有关JToolBar文档的信息,请参见此处

Initialize toolbars 初始化工具栏

 private JToolBar toolbar1;
 private JToolBar toolbar2;

You try to add buttons to your toolbars before you've created them. 您尝试在创建按钮之前将按钮添加到工具栏。 The simplest solution: 最简单的解决方案:

 private JToolBar toolbar1 = new JToolBar();
 private JToolBar toolbar2 = new JToolBar();

You should never catch the NullPointerException, you should always write your program such that it does not happen. 您永远不应捕获NullPointerException,而应始终编写不会发生的程序。 Apply necessary null checks for the conditions that are mentioned by "The Elite Gentlemen" :) 对“精英先生们”提到的条件进行必要的空检查:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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