Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当获取随机数时,我得到java.lang.NullPointerException_Java_Nullpointerexception - Fatal编程技术网

当获取随机数时,我得到java.lang.NullPointerException

当获取随机数时,我得到java.lang.NullPointerException,java,nullpointerexception,Java,Nullpointerexception,我试图为我的Java程序获取一个随机数,但是什么都没有发生,当我运行程序时,我得到了一个Java.lang.NullPointerException。。。这是我得到随机整数的代码部分,之后我将提供完整的代码。提前感谢您的帮助 try { PrintStream oFile = new PrintStream("Cipher.txt"); //i get the random number right below here...

我试图为我的Java程序获取一个随机数,但是什么都没有发生,当我运行程序时,我得到了一个Java.lang.NullPointerException。。。这是我得到随机整数的代码部分,之后我将提供完整的代码。提前感谢您的帮助

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

            } 
试试看{
PrintStream of文件=新的PrintStream(“Cipher.txt”);
//我得到了下面的随机数。。。
rot=随机。nextInt(5)+1;
扫描=新扫描仪(信息);
while(scan.hasNext()){
cipherWord=scan.next();
l=密码字长度();
charScan=新扫描仪(密码字);
for(int i=0;i
这是完整的程序代码

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);
    }
}
}
import java.awt.*;
导入java.awt.event.*;
导入java.util.*;
导入java.util.Random;
导入javax.swing.*;
导入java.io.*;
公共类代码生成器{
公共卫生;
公共JScrollPane滚动区;
公共随机;
公共文件密码;
公共国际;
公共文件信息;
公共字符-密码字符;
公共字符串finalCipher;
公共扫描仪;
公共扫描仪扫描;
公共字符串密码;
公共INTZ;
公共整型词;
公共文件加密文件;
公共int n;
公共字符串密码;
公共JPanel小组;
公共JButton保存按钮;
公共区域文本区域;
布尔创建=真;
私有JFrame;
公共静态void main(字符串[]args){
CodeMaker gui=新的CodeMaker();
gui.start();
}
私有void start(){
框架=新的JFrame(“战壕工程:密码创建者”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(颜色:深灰色);
makeMenus();
makeContent();
frame.setVisible(true);
frame.setExtendedState(frame.MAXIMIZED_两者);
}
私有void makemenu(){
梅努巴;
menuBar=newjmenubar();
frame.setJMenuBar(菜单栏);
JMenu菜单;
菜单=新JMenu(“文件”);
JMenuItem menuItem=新的JMenuItem(“新密码”);
menuItem.addActionListener(newNewListener());
menuItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_N,
事件。CTRL_掩码);
menu.add(menuItem);
JMenuItem menuitem=新的JMenuItem(“读取密码”);
menuitem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_R,
事件。CTRL_掩码);
menu.add(menuitem);
menu.addSeparator();
menuItem=新项目(“退出”);
menuItem.addActionListener(新的ExitListener());
menuItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_Q,
事件。CTRL_掩码);
menu.add(menuItem);
菜单栏。添加(菜单);
JMenu helpMenu=新JMenu(“帮助”);
JMenuItem helpItem=新的JMenuItem(“帮助”);
menuItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_H,
事件。CTRL_掩码);
帮助菜单。添加(帮助项);
菜单栏。添加(帮助菜单);
JMenu aboutMenu=新JMenu(“关于”);
JMenuItem aboutItem=新的JMenuItem(“关于”);
menuItem.setAccelerator
random = new Random();
Random random;
//later on somewhere
random = new Random();