我正在尝试为我的Java程序获取一个随机数,但没有任何反应,当我运行程序时,我得到一个java.lang.NullPointerException ...以下是我得到随机int的代码部分,I之后会提供完整的代码。在此先感谢您的帮助!!!
try {
PrintStream oFile = new PrintStream("Cipher.txt");
//i get the random number right below here...
rot = random.nextInt(5) + 1;
scan = new Scanner(message);
while (scan.hasNext()) {
cipherWord = scan.next();
l = cipherWord.length();
charScan = new Scanner(cipherWord);
for(int i = 0; i < cipherWord.length(); i++){
cipherChar = cipherWord.charAt(i);
if (cipherChar == 'a') {
cipherChar = 'b';
} else if (cipherChar == 'b') {
cipherChar = 'c';
} else if (cipherChar == 'c') {
cipherChar = 'd';
} else if (cipherChar == 'd') {
cipherChar = 'e';
} else if (cipherChar == 'e') {
cipherChar = 'f';
} else if (cipherChar == 'f') {
cipherChar = 'g';
} else if (cipherChar == 'g') {
cipherChar = 'h';
} else if (cipherChar == 'h') {
cipherChar = 'i';
} else if (cipherChar == 'i') {
cipherChar = 'j';
} else if (cipherChar == 'j') {
cipherChar = 'k';
} else if (cipherChar == 'k') {
cipherChar = 'l';
} else if (cipherChar == 'l') {
cipherChar = 'm';
} else if (cipherChar == 'm') {
cipherChar = 'n';
} else if (cipherChar == 'n') {
cipherChar = 'o';
} else if (cipherChar == 'o') {
cipherChar = 'p';
} else if (cipherChar == 'p') {
cipherChar = 'q';
} else if (cipherChar == 'q') {
cipherChar = 'r';
} else if (cipherChar == 'r') {
cipherChar = 's';
} else if (cipherChar == 's') {
cipherChar = 't';
} else if (cipherChar == 't') {
cipherChar = 'u';
} else if (cipherChar == 'u') {
cipherChar = 'v';
} else if (cipherChar == 'v') {
cipherChar = 'w';
} else if (cipherChar == 'w') {
cipherChar = 'x';
} else if (cipherChar == 'x') {
cipherChar = 'y';
} else if (cipherChar == 'y') {
cipherChar = 'z';
} else if (cipherChar == 'z') {
cipherChar = 'a';
}
oFile.print(cipherChar);
}
oFile.print(" ");
}
oFile.close();
scan.close();
charScan.close();
}
catch(Exception ioe) {
System.out.println("\n*** Print to Cipher.txt Error ***\n" + ioe);
}
这是完整的程序代码......
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;
import javax.swing.*;
import java.io.*;
public class CodeMaker {
public int rot;
public JScrollPane scrollArea;
public Random random;
public File Cipher;
public int l;
public File message;
public char cipherChar;
public String finalCipher;
public Scanner charScan;
public Scanner scan;
public String cipherWord;
public int z;
public int words;
public File cipherFile;
public int n;
public String cipher;
public JPanel panel;
public JButton saveButton;
public JTextArea textArea;
boolean create = true;
private JFrame frame;
public static void main (String[] args) {
CodeMaker gui = new CodeMaker();
gui.start();
}
private void start() {
frame = new JFrame("Project TrenchCoat: Cipher Creator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.DARK_GRAY);
makeMenus();
makeContent();
frame.setVisible(true);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
}
private void makeMenus() {
JMenuBar menuBar;
menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu menu;
menu = new JMenu("File");
JMenuItem menuItem = new JMenuItem("New Cipher");
menuItem.addActionListener(new NewListener());
menuItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_N,
Event.CTRL_MASK));
menu.add(menuItem);
JMenuItem menuitem = new JMenuItem("Read Cipher");
menuitem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_R,
Event.CTRL_MASK));
menu.add(menuitem);
menu.addSeparator();
menuItem = new JMenuItem("Exit");
menuItem.addActionListener(new ExitListener());
menuItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_Q,
Event.CTRL_MASK));
menu.add(menuItem);
menuBar.add(menu);
JMenu helpMenu = new JMenu("Help");
JMenuItem helpItem = new JMenuItem("Help");
menuItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_H,
Event.CTRL_MASK));
helpMenu.add(helpItem);
menuBar.add(helpMenu);
JMenu aboutMenu = new JMenu("About");
JMenuItem aboutItem = new JMenuItem("About");
menuItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_A,
Event.CTRL_MASK));
aboutMenu.add(aboutItem);
menuBar.add(aboutMenu);
}
private void makeContent() {
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
JLabel label = new JLabel("Click a button to use its function");
label.setForeground(Color.white);
label.setFont(new Font("Lucida Console", Font.PLAIN, 14));
contentPane.add(label, BorderLayout.NORTH);
panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
panel.setBackground(Color.DARK_GRAY);
JButton createButton = new JButton("Create Cipher");
createButton.addActionListener(new NewListener());
createButton.setBackground(Color.DARK_GRAY);
createButton.setFont(new Font("Lucida Console", Font.PLAIN, 14));
createButton.setForeground(Color.green);
panel.add(createButton);
JButton readButton = new JButton("Read Cipher");
readButton.addActionListener(new ReadListener());
readButton.setBackground(Color.DARK_GRAY);
readButton.setFont(new Font("Lucida Console", Font.PLAIN, 14));
readButton.setForeground(Color.green);
panel.add(readButton);
JButton aboutButton = new JButton("About Cipher Creator.......");
aboutButton.setBackground(Color.DARK_GRAY);
aboutButton.setFont(new Font("Lucida Console", Font.PLAIN, 14));
aboutButton.setForeground(Color.green);
panel.add(aboutButton);
JButton helpButton = new JButton("Help");
helpButton.setBackground(Color.DARK_GRAY);
helpButton.setFont(new Font("Lucida Console", Font.PLAIN, 14));
helpButton.setForeground(Color.green);
panel.add(helpButton);
frame.add(panel, BorderLayout.WEST);
JButton saveButton = new JButton("Save Cipher");
saveButton.addActionListener(new SaveListener());
saveButton.setBackground(Color.DARK_GRAY);
saveButton.setFont(new Font("Lucida Console", Font.PLAIN, 14));
saveButton.setForeground(Color.green);
panel.add(saveButton);
textArea = new JTextArea(0,0);
scrollArea = new JScrollPane(textArea);
scrollArea.setVerticalScrollBarPolicy(
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollArea.setHorizontalScrollBarPolicy(
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(scrollArea);
contentPane.add(textArea, BorderLayout.CENTER);
}
private class NewListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
textArea.setText("");
textArea.append("Type your message to be ciphered below, then click 'Save Cipher'. Remember, everything in the text area will be in your message, so delete this line.\n");
}
}
private class SaveListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
createCipher();
}
}
private class ReadListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
textArea.setText("");
JFileChooser fc = new JFileChooser();
fc.showOpenDialog(frame);
cipherFile = fc.getSelectedFile();
if (cipherFile == null) {
return;
}
readCipher();
}
}
private void readCipher() {
try
{
Scanner scan = new Scanner(cipherFile);
while (scan.hasNext())
{
String cipher = scan.next();
textArea.append(cipher);
textArea.append("\n");
}
scan.close();
}
catch(IOException e)
{
JOptionPane.showMessageDialog(frame,
"I/O error in file\n\n " +
cipherFile.getName() +
"\n\nThis program will close",
"I/O Error",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
private void createCipher() {
try
{
cipher = textArea.getText();
PrintStream oFile = new PrintStream("Message.txt");
message = new File("Message.txt");
oFile.print(cipher);
oFile.close();
}
catch(IOException ioe)
{
System.out.println("\n*** I/O Error ***\n" + ioe);
}
try {
PrintStream oFile = new PrintStream("Cipher.txt");
rot = random.nextInt(5) + 1;
scan = new Scanner(message);
while (scan.hasNext()) {
cipherWord = scan.next();
l = cipherWord.length();
charScan = new Scanner(cipherWord);
for(int i = 0; i < cipherWord.length(); i++){
cipherChar = cipherWord.charAt(i);
if (cipherChar == 'a') {
cipherChar = 'b';
} else if (cipherChar == 'b') {
cipherChar = 'c';
} else if (cipherChar == 'c') {
cipherChar = 'd';
} else if (cipherChar == 'd') {
cipherChar = 'e';
} else if (cipherChar == 'e') {
cipherChar = 'f';
} else if (cipherChar == 'f') {
cipherChar = 'g';
} else if (cipherChar == 'g') {
cipherChar = 'h';
} else if (cipherChar == 'h') {
cipherChar = 'i';
} else if (cipherChar == 'i') {
cipherChar = 'j';
} else if (cipherChar == 'j') {
cipherChar = 'k';
} else if (cipherChar == 'k') {
cipherChar = 'l';
} else if (cipherChar == 'l') {
cipherChar = 'm';
} else if (cipherChar == 'm') {
cipherChar = 'n';
} else if (cipherChar == 'n') {
cipherChar = 'o';
} else if (cipherChar == 'o') {
cipherChar = 'p';
} else if (cipherChar == 'p') {
cipherChar = 'q';
} else if (cipherChar == 'q') {
cipherChar = 'r';
} else if (cipherChar == 'r') {
cipherChar = 's';
} else if (cipherChar == 's') {
cipherChar = 't';
} else if (cipherChar == 't') {
cipherChar = 'u';
} else if (cipherChar == 'u') {
cipherChar = 'v';
} else if (cipherChar == 'v') {
cipherChar = 'w';
} else if (cipherChar == 'w') {
cipherChar = 'x';
} else if (cipherChar == 'x') {
cipherChar = 'y';
} else if (cipherChar == 'y') {
cipherChar = 'z';
} else if (cipherChar == 'z') {
cipherChar = 'a';
}
oFile.print(cipherChar);
}
oFile.print(" ");
}
oFile.close();
scan.close();
charScan.close();
}
catch(Exception ioe) {
System.out.println("\n*** Print to Cipher.txt Error ***\n" + ioe);
}
viewCipher();
}
private void viewCipher() {
textArea.append("\nBelow is the cipher you created.\n Now you can copy/paste in and email it to the desired recipient.");
try
{
File Cipher = new File("Cipher.txt");
Scanner scan = new Scanner(Cipher);
textArea.append("\n");
while (scan.hasNextLine())
{
String cipher = scan.nextLine();
textArea.append(cipher);
textArea.append("\n");
}
}
catch(IOException e)
{
JOptionPane.showMessageDialog(frame,
"I/O error in file\n\n " +
cipherFile.getName() +
"\n\nThis program will close",
"I/O Error",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
private class ExitListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
}
答案 0 :(得分:6)
您的随机生成器随机未初始化。初始化它。
random = new Random();
顺便说一下,下次将问题平分并仅发布有问题的部分。
答案 1 :(得分:3)
我认为您需要初始化random
变量:
Random random;
//later on somewhere
random = new Random();