在Java中编译很好,但运行会导致错误

时间:2014-06-07 02:44:34

标签: java runtime-error

好的,所以我做了javac dicebot.java,它工作正常,但是当我做了java dicebot.java我得到了这个: 考虑到我不知道这些错误是什么意思,我真的不知道能解释得多好多少。所以任何帮助都会很棒,谢谢!

与你在那里提出的问题不同,因为它与它无关。这是一个完全不同的问题,它的错误是独一无二的......

java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1086)
at java.awt.Container.add(Container.java:966)
at dicebot.<init>(dicebot.java:67)
at dicebot$1.run(dicebot.java:26)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

这是我制作并尝试运行的编码:

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class dicebot extends JFrame implements ActionListener {
    static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;
    public static JComboBox combo;
    public static JButton btnConfirm;
    public static JTextField txtUserName;
    public static JTextField txtStartBid;
    public static JTextField txtMultiplier;
    public static JTextField txtMinRemaining;
    public static JTextField txtPassword;
    public static JTextField txtOdds;
    public static JTextField txtMaxBet;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    dicebot frame = new dicebot();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public dicebot() {
        setTitle("Dice Bot");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.WEST);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[]{0, 0};
        gbl_panel.rowHeights = new int[]{0, 0};
        gbl_panel.columnWeights = new double[]{0.0, 1.0};
        gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
        panel.setLayout(gbl_panel);

        //Every new Label however needs every part that says "user" or on the Password: "pass" changed to something unique.
        JLabel userTag = new JLabel("Username:");
        GridBagConstraints gbc_userTag = new GridBagConstraints();
        gbc_userTag.insets = new Insets(0, 0, 0, 5);
        gbc_userTag.anchor = GridBagConstraints.EAST;
        gbc_userTag.gridx = 0;//Here are your x + y coords
        gbc_userTag.gridy = 0;//Adding to x moves left, adding to y moves down
        panel.add(userTag, gbc_userTag);

        //Every new textfield needs only the * part to change for it to be valid. (gbc_* =)
        txtUserName = new JTextField();
        GridBagConstraints txtUserName = new GridBagConstraints();
        txtUserName.fill = GridBagConstraints.HORIZONTAL;
        txtUserName.gridx = 1;
        txtUserName.gridy = 0;
        panel.add(textField,txtUserName);
        textField.setColumns(10);

        JLabel startTag = new JLabel("Starting Bid:");
        GridBagConstraints gbc_startTag = new GridBagConstraints();
        gbc_startTag.insets = new Insets(0, 0, 0, 5);
        gbc_startTag.anchor = GridBagConstraints.EAST;
        gbc_startTag.gridx = 0;
        gbc_startTag.gridy = 2;
        panel.add(startTag, gbc_startTag);

        txtStartBid = new JTextField();
        GridBagConstraints txtStartBid = new GridBagConstraints();
        txtStartBid.fill = GridBagConstraints.HORIZONTAL;
        txtStartBid.gridx = 1;
        txtStartBid.gridy = 2;
        panel.add(textField, txtStartBid);
        textField.setColumns(10);

        JLabel multTag = new JLabel("Multiplier:");
        GridBagConstraints gbc_multTag = new GridBagConstraints();
        gbc_multTag.insets = new Insets(0, 0, 0, 5);
        gbc_multTag.anchor = GridBagConstraints.EAST;
        gbc_multTag.gridx = 0;
        gbc_multTag.gridy = 3;
        panel.add(multTag, gbc_multTag);

        txtMultiplier = new JTextField();
        GridBagConstraints txtMultiplier = new GridBagConstraints();
        txtMultiplier.fill = GridBagConstraints.HORIZONTAL;
        txtMultiplier.gridx = 1;
        txtMultiplier.gridy = 3;
        panel.add(textField, txtMultiplier);
        textField.setColumns(10);

        JLabel minTag = new JLabel("Min Remaining:");
        GridBagConstraints gbc_minTag = new GridBagConstraints();
        gbc_minTag.insets = new Insets(0, 0, 0, 5);
        gbc_minTag.anchor = GridBagConstraints.EAST;
        gbc_minTag.gridx = 0;
        gbc_minTag.gridy = 4;
        panel.add(minTag, gbc_minTag);

        txtMinRemaining = new JTextField();
        GridBagConstraints txtMinRemaining = new GridBagConstraints();
        txtMinRemaining.fill = GridBagConstraints.HORIZONTAL;
        txtMinRemaining.gridx = 1;
        txtMinRemaining.gridy = 4;
        panel.add(textField, txtMinRemaining);
        textField.setColumns(10);

        txtPassword = new JTextField();
        GridBagConstraints txtPassword = new GridBagConstraints();
        txtPassword.fill = GridBagConstraints.HORIZONTAL;
        txtPassword.gridx = 1;
        txtPassword.gridy = 1;
        panel.add(textField, txtPassword);
        textField.setColumns(10);

        JLabel passTag = new JLabel("Password:");
        GridBagConstraints gbc_passTag = new GridBagConstraints();
        gbc_passTag.insets = new Insets(0, 0, 0, 5);
        gbc_passTag.anchor = GridBagConstraints.EAST;
        gbc_passTag.gridx = 0;
        gbc_passTag.gridy = 1;
        panel.add(passTag, gbc_passTag);

        txtOdds = new JTextField();
        GridBagConstraints txtOdds = new GridBagConstraints();
        txtOdds.fill = GridBagConstraints.HORIZONTAL;
        txtOdds.gridx = 1;
        txtOdds.gridy = 5;
        panel.add(textField, txtOdds);
        textField.setColumns(10);

        JLabel oddsTag = new JLabel("Odds %:");
        GridBagConstraints gbc_oddsTag = new GridBagConstraints();
        gbc_oddsTag.insets = new Insets(0, 0, 0, 5);
        gbc_oddsTag.anchor = GridBagConstraints.EAST;
        gbc_oddsTag.gridx = 0;
        gbc_oddsTag.gridy = 5;
        panel.add(oddsTag, gbc_oddsTag);

        txtMaxBet = new JTextField();
        GridBagConstraints txtMaxBet = new GridBagConstraints();
        txtMaxBet.fill = GridBagConstraints.HORIZONTAL;
        txtMaxBet.gridx = 1;
        txtMaxBet.gridy = 6;
        panel.add(textField, txtMaxBet);
        textField.setColumns(10);

        //This is the Combo Box
        combo = new JComboBox<String>(new String[]{"BTC","LTC","PPC","NMC","XPM","FTC","ANC","DOGE","NXT"});
        combo.addActionListener(this);
        GridBagConstraints gbc_list = new GridBagConstraints();
        gbc_list.fill = GridBagConstraints.HORIZONTAL;
        gbc_list.gridx = 1;
        gbc_list.gridy = 7;
        panel.add(combo, gbc_list);

        JLabel maxTag = new JLabel("MaxBet:");
        GridBagConstraints gbc_maxTag = new GridBagConstraints();
        gbc_maxTag.insets = new Insets(0, 0, 0, 5);
        gbc_maxTag.anchor = GridBagConstraints.EAST;
        gbc_maxTag.gridx = 0;
        gbc_maxTag.gridy = 6;
        panel.add(maxTag, gbc_maxTag);

        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, BorderLayout.SOUTH);
        panel_1.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));

        btnConfirm = new JButton("Turn Up");
    btnConfirm.addActionListener(this);
    panel_1.add(btnConfirm);

    JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane, BorderLayout.CENTER);
        JTextArea textArea = new JTextArea("Current Balance");
        textArea.setColumns(1);
        scrollPane.setViewportView(textArea);

    JScrollPane scrollPanel1 = new JScrollPane();//This will hold the information the bot sends over such as win/loose or error
        contentPane.add(scrollPane, BorderLayout.CENTER);
        JTextArea textAreal = new JTextArea("Input bot information here...");
        textArea.setColumns(20);
        scrollPane.setViewportView(textArea);

        pack();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == combo) {
            System.out.println(combo.getSelectedIndex()+1);
        }
    }

}

2 个答案:

答案 0 :(得分:2)

NullPointerException表示有些东西试图调用方法或访问null对象的变量。

根据你的评论,

会抛出异常
panel.add(textField,txtUserName);

现在,txtUserName之前被初始化了几行,因此它不应该是原因,textField变量怎么样?

答案 1 :(得分:0)

正如杰克指出的那样,textField未初始化。