Java,无法在同一个类中访问方法

时间:2013-01-09 02:17:51

标签: java

我的问题出现在调用buildInven(bigPath.getText())时,我不断得到一个

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

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

CODE:

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

2 个答案:

答案 0 :(得分:3)

问题是您的bigPath 字段仍然为空。在构造函数中,您声明了一个 local 变量并初始化它:

JTextArea bigPath = new JTextArea(...);

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

bigPath.getText()

...你得到NullPointerException

只需将上述内容更改为:

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

...并对bigOklilPathlilOk执行相同操作,您也会遇到同样的错误。

您的代码中不一定所有都是错误的,但这就是您获得异常的原因。

答案 1 :(得分: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);

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

相关问题