私はJava(およびこのサイト)にかなり慣れていないので、この「問題」をどうすればよいかわかりません...コード全体をこれに入れるかどうかわかりません...だから私は問題を引き起こしていると思われるものを入れます。プログラムは、実行しようとする場合を除いて、完全にコンパイルされます。そのとき、96 行目で Null Pointer Exception が発生します。なぜこれが起こっているのかわかりません。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class LicensePlateGeneratorTemplate extends JFrame implements ActionListener{
private JTextArea txtOutput;
private JTextField txtNumPlates;
private JButton btnDisplay;
private JLabel lblTitle, lblInstruction;
private PrintWriter output;
private String letters, numbers, fileName;
private int randomLetter, randomNumber, numPlates;
private String [] a, b ;
private String [] lttrArray, nmbrArray;
public static void main(String[] args) throws IOException{
new LicensePlateGeneratorTemplate();
}
public LicensePlateGeneratorTemplate() throws IOException{
//label thast contains number of licence plates generated
txtNumPlates = new JTextField();
txtNumPlates.setBounds(320, 60, 28, 28);
lblInstruction = new JLabel("How many license plates would you like to generate?");
lblInstruction.setBounds(15, 60, 300, 28);
//A random number generator in the action performed method should allow you to select a random letter from the letters String
//then a random number from the numbers String
String letters = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Z";
String numbers = "1,2,3,4,5,6,7,8,9,0";
lttrArray = letters.split (",");
nmbrArray = numbers.split (",");
System.out.println(lttrArray[20] + lttrArray [10]);
System.out.println(nmbrArray[3]+ nmbrArray [5]);
fileName = "licensePlates.txt";
output = new PrintWriter(new FileWriter(fileName));
lblTitle = new JLabel();
lblTitle.setFont(new Font("Britannic Bold", Font.BOLD, 28));
lblTitle.setText("License Plate Generator");
lblTitle.setBounds(30, 10, 400, 50);
JPanel panel = new JPanel();
panel.setLayout(null);
//JTextArea
txtOutput = new JTextArea();
txtOutput.setBounds(60, 150, 275, 196);
txtOutput.setEditable(true);
//generate button properties
btnDisplay = new JButton();
btnDisplay.setText("Generate");
btnDisplay.setBounds(150, 100, 100, 40);
btnDisplay.setFocusable(true);
btnDisplay.addActionListener(this);
//add components to the panel
panel.add(btnDisplay);
panel.add(txtOutput);
panel.add(lblTitle);
panel.add(lblInstruction);
panel.add(txtNumPlates);
//frame properties
setContentPane(panel);
setSize(400, 400);
setTitle("License Plate Generator");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public void actionPerformed (ActionEvent e)
{
if (e.getSource() == btnDisplay)
{
//change number user inputs into a int value
int userNum = Integer.parseInt(txtNumPlates.getText ());
for (int k = 0; k <= userNum; k ++)
{//number of licence places that are being made
for (int i = 0; i < 4; i ++)//loop for letters produced
{
int randLtr = 0 + (int)(Math.random() * ((24 - 0) + 1));
a [i] = lttrArray [randLtr];
output.println (a[i]);
txtOutput.insert(a[i] + "\n", 0);
}
for (int j = 0; j < 3; j++)//loop for number produced
{
int randNum = 0 + (int)(Math.random() * ((9 - 0) + 1));
b [j] = nmbrArray [randNum];
output.println (b[j]);
txtOutput.insert(b[j] + "\n", 0);
}
output.close();//closes fileWriter
}//end of userNum loop
}//end of e.getsource
}
}
編集:完全なコードを追加しました。