0

调用 buildInven(bigPath.getText()) 时出现问题,我不断收到

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TestLayout06$InputPanel.actionPerformed(TestLayout06.java:112)

抛出。任何想法我的问题可能是什么?

代码:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout06 {

public static void main(String[] args) 
{
    new TestLayout06();
}

public TestLayout06() 
{
    EventQueue.invokeLater(new Runnable() 
    {
        @Override
        public void run() 
        {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException ex) {
            } catch (InstantiationException ex) {
            } catch (IllegalAccessException ex) {
            } catch (UnsupportedLookAndFeelException ex) {
            }

            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(BorderLayout.NORTH, new InputPanel());
            frame.add(BorderLayout.CENTER, new InfoPanel());
            frame.add(BorderLayout.SOUTH, new CLInfoPanel());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setSize(500, 500);
            frame.setVisible(true);

        }            
    });
}

protected class InputPanel extends JPanel implements ActionListener {

    int InventorySize = 2000;
    String AItem[] = new String[InventorySize];
    String Active[] = new String[InventorySize];
    String IdNum[] = new String[InventorySize];

    String Item[] = new String[InventorySize];
    String BNumber[] = new String[InventorySize];
    String Descrip[] = new String[InventorySize];
    String Retail[]  = new String[InventorySize];
    String Disc[] = new String[InventorySize];
    String Price[]  = new String[InventorySize];

    JTextArea bigPath = null;
    JButton bigOk = null;
    JTextArea lilPath = null;
    JButton lilOk = null;

    public InputPanel() 
    {            
        JLabel label = new JLabel("Menu");
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(label, gbc);

        JTextArea bigPath = new JTextArea("C:\\Users\\User\\Documents\\InputInventory.txt", 1, 30);
        JButton bigOk = new JButton("Load Inventory");
        JTextArea lilPath = new JTextArea("C:\\Users\\User\\Documents\\ItemsMarked.txt", 1, 30);
        JButton lilOk = new JButton("Load Info");

        bigOk.addActionListener(this);
        lilOk.addActionListener(this);


        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridy++;
        add(bigPath, gbc);
        gbc.gridy++;
        add(bigOk, gbc);
        gbc.gridy++;
        add(lilPath, gbc);
        gbc.gridy++;
        add(lilOk, gbc);
    }

    public void actionPerformed(ActionEvent evt) 
    {
        String str = evt.toString();
        boolean inv = str.contains("Load Inventory");
        boolean item = str.contains("Load Info");
        if (inv)
            this.buildInven(this.bigPath.getText());
        if (item)
            this.buildCLInf(lilPath.getText());
    }

    public void buildInven(String arg)
    {
        Scanner in = null;
        try
        {
            in = new Scanner(new File(arg));
        }
        catch (FileNotFoundException e)
        {
            bigPath.setText(arg + " not found");
        }
        int i = 0;
        while (in.hasNextLine())
        {
            int j = 0;
            StringTokenizer tkner = new StringTokenizer(in.nextLine());
            while (tkner.hasMoreTokens())
            {
                if(j == 0)
                    Item[i] = tkner.nextToken("|");
                else if(j == 1)
                    BNumber[i] = tkner.nextToken("|");
                else if(j == 2)
                    Descrip[i] = tkner.nextToken("|");
                else if(j == 3)
                    Retail[i] = tkner.nextToken("|");
                else if(j == 3)
                    Disc[i] = tkner.nextToken("|");
                else if(j == 5)
                    Price[i] = tkner.nextToken("|");
                else
                    tkner.nextToken("|");
                 j++;
            }
            i++;
        }
        in.close();
    }

    public void buildCLInf(String arg)
    {
        Scanner in = null;
        try
        {
            in = new Scanner(new File(arg));
        }
        catch (FileNotFoundException e)
        {
            lilPath.setText(arg + " not found");
        }
        int i = 0;
        while (in.hasNextLine())
        {
            int j = 0;
            StringTokenizer tkner = new StringTokenizer(in.nextLine());
            while (tkner.hasMoreTokens())
            {
                if(j == 0)
                    AItem[i] = tkner.nextToken("|");
                else if(j == 1)
                    Active[i] = tkner.nextToken("|");
                else if(j == 2)
                    IdNum[i] = tkner.nextToken("|");
                else
                    tkner.nextToken("|");
                 j++;
            }
            i++;
        }
        in.close();
    }

}

protected class InfoPanel extends JPanel {

    public InfoPanel() {            
        JLabel label = new JLabel("Menu");
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(label, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridy++;
        add(new JButton("Option 1"), gbc);
        gbc.gridy++;
        add(new JButton("Option 2"), gbc);
        gbc.gridy++;
        add(new JButton("Option 3"), gbc);
    }
}

protected class CLInfoPanel extends JPanel {

    public CLInfoPanel() {            
        JLabel label = new JLabel("Menu");
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(label, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridy++;
        add(new JButton("Option 1"), gbc);
        gbc.gridy++;
        add(new JButton("Option 2"), gbc);
        gbc.gridy++;
        add(new JButton("Option 3"), gbc);
    }
}
}
4

2 回答 2

3

问题是您的bigPath 字段仍然为空。在您的构造函数中,您要声明一个局部变量并对其进行初始化:

JTextArea bigPath = new JTextArea(...);

这根本不会改变你的领域的价值。因此,该字段的值保持其默认值null,因此当您执行此代码时:

bigPath.getText()

...你得到NullPointerException.

只需将上面的内容更改为:

bigPath = new JTextArea(/* code as before */);

...并且对bigOk,lilPath也做同样的lilOk事情,在那里你有同样的错误。

这不一定是您的代码中的全部错误,但这就是您遇到异常的原因。

于 2013-01-09T02:19:48.067 回答
2

你得到了一个 NPE,因为你已经bigPath在方法中第二次声明了,因此你正在隐藏/隐藏你认为你正在使用的那个(在类中声明为一个字段的那个)。

改变:

JTextArea bigPath = new JTextArea("C:\\Users\\User\\Documents\\InputInventory.txt", 1, 30);

至:

bigPath = new JTextArea("C:\\Users\\User\\Documents\\InputInventory.txt", 1, 30);

这也适用于您在该范围内重新声明的所有其他变量。

于 2013-01-09T02:20:51.117 回答