Java,无法访问同一类中的方法

标签 java

当调用 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);
    }
}
}

最佳答案

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

JTextArea bigPath = new JTextArea(...);

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

bigPath.getText()

...您会得到NullPointerException

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

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

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

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

关于Java,无法访问同一类中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14227320/

相关文章:

java - 在保存实体之前避免 Spring Data JPA 选择

java - 将当前日期与属性文件中的日期进行比较

java - 如何在Java项目中使用Kotlin库?

java - 使用 SpinnerAdapter 自定义 Android Spinner XML?

java - 为具有可编辑列数和行数的 JTable 设置列名称

java - 需要正则表达式来替换文本中的 RTF 控制字。 java

java - 重构后更改序列化 Java 对象的路径/类名

java - 循环遍历字符数组并计算字符数

java - 我可以将 Node.js 应用程序打包为 Java EAR

java - 如何使用正则表达式提取 CSS 颜色?