私の問題は、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);
}
}
}