-7

I'm completely lost, being new to Java I can't seem to find what I need to do. Please, do not refer me to another person's question, those don't help because I don't know how it will fix my problem.

Here is my code:

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Locale;
import javax.swing.text.Element;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import json.JsonObject;
public class dicebot extends JFrame implements ActionListener {
    static final long serialVersionUID = 1L;
    private JPanel contentPane;
    public static String APIKey = null;
    public static JComboBox<String>cmbCurrency;
    public static JButton btnLow;
    public static JButton btnFloat;
    public static JButton btnHigh;
    public static JButton btnClearLog;
    public static JButton btnDonate;
    public static JTextPane textPane;
    public static JCheckBox scrollCheck;
    public static JCheckBox scrollDisable;
    public static JTextField txtRollAmnt;
    public static JTextField txtUserName;
    public static JTextField txtStartBid;
    public static JTextField txtMultiplier;
    public static JTextField txtMinRemaining;
    public static JPasswordField txtPassword;
    public static JTextField txtOdds;
    public static JTextField txtMaxBet;
    public static JTextArea txtInfo;
    public static JCheckBox RollAmntCheck;
    public static JLabel lblBalTag;
    public static JLabel userTag;
    public static JLabel passTag;
    public static void main(String[] args) {
        Locale.setDefault(Locale.US);
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    dicebot frame = new dicebot();
                    frame.setVisible(true);
                    Dicebotcode d = new Dicebotcode();
                    d.LoadSettings();
                    d = null;
                } 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.
        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 = 1;//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_* =)
          //textField = new JTextField();
        txtUserName = new JTextField();
        GridBagConstraints grdUserName = new GridBagConstraints();
        grdUserName.fill = GridBagConstraints.HORIZONTAL;
        grdUserName.gridx = 1;
        grdUserName.gridy = 1;
        txtUserName.setColumns(10);
        panel.add(txtUserName, grdUserName);
        //panel.add(textField,txtUserName);
        //textField.setColumns(10);

    JLabel balTag = new JLabel("Current Balance:");
        GridBagConstraints gbc_balTag = new GridBagConstraints();
        gbc_balTag.insets = new Insets(0, 0, 0, 5);
        gbc_balTag.anchor = GridBagConstraints.EAST;
        gbc_balTag.gridx = 0;  
        gbc_balTag.gridy = 0;
        panel.add(balTag, gbc_balTag);

        lblBalTag = new JLabel("[________________]");
        lblBalTag.setToolTipText("Balance as of the last call to the peerbet site.");
        GridBagConstraints gbc_lblBalTag = new GridBagConstraints();
        gbc_lblBalTag.insets = new Insets(0, 0, 0, 5);
        gbc_lblBalTag.anchor = GridBagConstraints.EAST;
        gbc_lblBalTag.gridx = 1;
        gbc_lblBalTag.gridy = 0;
        panel.add(lblBalTag, gbc_lblBalTag);

        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 = 3;
        panel.add(startTag, gbc_startTag);

        txtStartBid = new JTextField();
        GridBagConstraints grdStartBid = new GridBagConstraints();
        grdStartBid.fill = GridBagConstraints.HORIZONTAL;
        grdStartBid.gridx = 1;
        grdStartBid.gridy = 3;
        txtStartBid.setText("0.00000010");
        txtStartBid.setEnabled(false);
        panel.add(txtStartBid, grdStartBid);

        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 = 4;
        panel.add(multTag, gbc_multTag);

        txtMultiplier = new JTextField();
        GridBagConstraints grdMultiplier = new GridBagConstraints();
        grdMultiplier.fill = GridBagConstraints.HORIZONTAL;
        grdMultiplier.gridx = 1;
        grdMultiplier.gridy = 4;
        txtMultiplier.setColumns(10);
        txtMultiplier.setText("2");
        txtMultiplier.setEnabled(false);
        panel.add(txtMultiplier, grdMultiplier);

        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 = 5;
        panel.add(minTag, gbc_minTag);

        txtMinRemaining = new JTextField();
        GridBagConstraints grdMinRemaining = new GridBagConstraints();
        grdMinRemaining.fill = GridBagConstraints.HORIZONTAL;
        grdMinRemaining.gridx = 1;
        grdMinRemaining.gridy = 5;
        txtMinRemaining.setColumns(10);
        txtMinRemaining.setText("0");
        txtMinRemaining.setEnabled(false);
        panel.add(txtMinRemaining, grdMinRemaining);

        txtPassword = new JPasswordField();
        GridBagConstraints grdPassword = new GridBagConstraints();
        grdPassword.fill = GridBagConstraints.HORIZONTAL;
        grdPassword.gridx = 1;
        grdPassword.gridy = 2;
        txtPassword.setEchoChar('*');
        txtPassword.setColumns(10);
        panel.add(txtPassword, grdPassword);

        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 = 2;
        panel.add(passTag, gbc_passTag);

        txtOdds = new JTextField();
        GridBagConstraints grdOdds = new GridBagConstraints();
        grdOdds.fill = GridBagConstraints.HORIZONTAL;
        grdOdds.gridx = 1;
        grdOdds.gridy = 6;
        txtOdds.setColumns(10);
        txtOdds.addActionListener(this);
        txtOdds.setText("49.5");
        txtOdds.setEnabled(false);
        panel.add(txtOdds, grdOdds);

        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 = 6;
        panel.add(oddsTag, gbc_oddsTag);

        txtMaxBet = new JTextField();
        GridBagConstraints grdMaxBet = new GridBagConstraints();
        grdMaxBet.fill = GridBagConstraints.HORIZONTAL;
        grdMaxBet.gridx = 1;
        grdMaxBet.gridy = 7;
        txtMaxBet.setColumns(10);
        txtMaxBet.setText("1");
        txtMaxBet.setEnabled(false);
        panel.add(txtMaxBet, grdMaxBet);

    txtRollAmnt = new JTextField();
        GridBagConstraints grdRollAmnt = new GridBagConstraints();
        grdRollAmnt.fill = GridBagConstraints.HORIZONTAL;
        grdRollAmnt.gridx = 1;
        grdRollAmnt.gridy = 8;
        txtRollAmnt.setColumns(10);
        txtRollAmnt.setText("0=Infinite");
        txtRollAmnt.setEnabled(false);
        panel.add(txtRollAmnt, grdRollAmnt);

    RollAmntCheck = new JCheckBox("Roll Then Quit:");   
        RollAmntCheck.setSelected(true);
    GridBagConstraints grdRollAmntCheck = new GridBagConstraints();
        grdRollAmntCheck.fill = GridBagConstraints.HORIZONTAL;
        grdRollAmntCheck.gridx = 0;
        grdRollAmntCheck.gridy = 8;
        panel.add(RollAmntCheck, grdRollAmntCheck);

        //This is the Combo Box
        cmbCurrency = new JComboBox<String>(new String[]{"BTC","LTC","PPC","NMC","XPM","FTC","ANC","DOGE","NXT"});
        GridBagConstraints gbc_list = new GridBagConstraints();
        gbc_list.fill = GridBagConstraints.HORIZONTAL;
        gbc_list.gridx = 1;
        gbc_list.gridy = 9;
        cmbCurrency.addActionListener(this);
        cmbCurrency.setEnabled(false);
        panel.add(cmbCurrency, 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 = 7;
        panel.add(maxTag, gbc_maxTag);

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

        btnDonate = new JButton("Login");
        btnDonate.addActionListener(this);
        panel_1.add(btnDonate);

        btnHigh = new JButton("Roll High");
        btnHigh.addActionListener(this);
        btnHigh.setEnabled(false);
        panel_1.add(btnHigh);

        btnLow = new JButton("Roll Low");
        btnLow.addActionListener(this);
        btnLow.setEnabled(false);
        panel_1.add(btnLow);

        btnFloat = new JButton("Roll Float");
        btnFloat.addActionListener(this);
        btnFloat.setEnabled(false);
        btnFloat.setVisible(false);
        panel_1.add(btnFloat);

        btnClearLog = new JButton("Clear Log");
        btnClearLog.addActionListener(this);
        panel_1.add(btnClearLog);

        scrollCheck = new JCheckBox("Auto-Scroll"); 
        scrollCheck.setSelected(true);
        panel_1.add(scrollCheck);

        scrollDisable = new JCheckBox("Disable Log");   
        scrollDisable.setSelected(false);
        panel_1.add(scrollDisable);

        btnClearLog.setToolTipText("Click here to clear the log!");
        btnHigh.setToolTipText("Click here to Roll High!");
        btnLow.setToolTipText("Click here to Roll Low!");
        btnFloat.setToolTipText("Click here to Roll?");
        scrollCheck.setToolTipText("Toggles the auto-scroll function of the log.");
        RollAmntCheck.setToolTipText("Roll Amount then Quit");
        txtMaxBet.setToolTipText("The dicebot will not bet above amount entered in.");
        txtOdds.setToolTipText("What odds(%) will the dicebot be rolling?");
        txtPassword.setToolTipText("Enter your peerbet account password.");
        txtMinRemaining.setToolTipText("The bot will stop when account has less than this amount in bank.");
        txtMultiplier.setToolTipText("What shall the bet be multiplied by upon loss?");
        txtStartBid.setToolTipText("What amount should the bot start each bet at?");
        txtUserName.setToolTipText("Enter your peerbet account username.");
        lblBalTag.setToolTipText("Current amount of chosen currency shown here.");
        cmbCurrency.setToolTipText("Choose the currency that the bot will be using to roll with.");

         contentPane.add(textPane, BorderLayout.CENTER);
        txtInfo = new JTextArea("All number formats must use a period(.)\nBot By: MichaelAdair and DalinSprocket\n");
        txtInfo.setColumns(35);
        txtInfo.setEnabled(false);

    textPane = new JTextPane();
        textPane.setBackground(Color.DARK_GRAY);
        textPane.setEditable(false);
        textPane.setMargin(null);
        textPane.setContentType("text/html");

    StyledDocument doc = textPane.getStyledDocument();

        Style style = textPane.addStyle("Loss",null);
        StyleConstants.setForeground(style, Color.red);

    Style style2 = textPane.addStyle("Win",null);
        StyleConstants.setForeground(style, Color.green);
        pack();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == cmbCurrency) {
            if (cmbCurrency.getSelectedIndex() == 0){
                txtStartBid.setText("0.00000010");
            }else{
                txtStartBid.setText("0.0001");
            }
            if(APIKey != null){
                String balance = peerbetapi.get_balance(dicebot.APIKey);
                JsonObject jsonObject = JsonObject.readFrom(balance);
                if(jsonObject.get("status").asInt() == 1){
                    lblBalTag.setText(jsonObject.get("raffle_cur" + Integer.toString((cmbCurrency.getSelectedIndex() + 10))).asString());
                }
            }else{
                lblBalTag.setText("[________________]");
            }
        }else if (e.getSource() == btnLow){
            if(btnLow.getText() == "Roll Low"){
                btnHigh.setText("Stop");
                btnLow.setText("Stop On Win");
                btnFloat.setEnabled(false);
                Dicebotcode dbc = new Dicebotcode();
                Dicebotcode.RollType = "low";
                Dicebotcode.StopRollingOnWin = false;
                Dicebotcode.StopRolling = false;
                dbc.dbc();
            }else{
                // The EnableAllFields function will re-enable the buttons once its done.
                btnLow.setText("Waiting...");
                btnLow.setEnabled(false);
                Dicebotcode.StopRollingOnWin = true;
            }
        }else if (e.getSource() == btnHigh){
            if(btnHigh.getText() == "Roll High"){
                btnHigh.setText("Stop");
                btnLow.setText("Stop On Win");
                btnFloat.setEnabled(false);
                Dicebotcode dbc = new Dicebotcode();
                Dicebotcode.RollType = "high";
                Dicebotcode.StopRollingOnWin = false;
                Dicebotcode.StopRolling = false;
                dbc.dbc();
            }else{
                // The EnableAllFields function will re-enable the buttons once its done.
                btnHigh.setText("Stopping...");
                btnHigh.setEnabled(false);
                btnLow.setEnabled(false);
                Dicebotcode.StopRolling = true;
            }
        }else if (e.getSource() == btnFloat){
            if(btnFloat.getText() == "Roll Float"){
                btnHigh.setText("Stop");
                btnLow.setText("Stop On Win");
                btnFloat.setEnabled(false);
                Dicebotcode dbc = new Dicebotcode();
                Dicebotcode.RollType = "float";
                Dicebotcode.StopRollingOnWin = false;
                Dicebotcode.StopRolling = false;
                dbc.dbc();
            }else{
                // The EnableAllFields function will re-enable the buttons once its done.
                btnFloat.setText("Stopping...");
                btnFloat.setEnabled(false);
                Dicebotcode.StopRolling = true;
            }
        }else if (e.getSource() == btnClearLog){
            txtInfo.setText("");
        }else if (e.getSource() == btnDonate){
            //donate d = new donate();
            if(btnDonate.getText() == "Login"){
                String reply = null;
                try {
                    reply = peerbetapi.login(txtUserName.getText(), String.copyValueOf(txtPassword.getPassword()));
                } catch (IOException e1) {
                    reply = "{\"status\":0, \"message\":\"An unknown error has occurred while attempting to login.\"}";
                }
                JsonObject json = JsonObject.readFrom(reply);
                if(json.get("status").asInt() != 1){
                    txtInfo.append("Error: " + json.get("message").asString() + "\n");
                    txtInfo.setCaretPosition(txtInfo.getText().length());
                }else{
                    APIKey = json.get("key").asString();
                    lblBalTag.setText(json.get("raffle_cur" + Integer.toString(cmbCurrency.getSelectedIndex() + 10)).asString());
                    btnDonate.setText("Donate");
                    userTag.setVisible(false);
                    txtUserName.setVisible(false);
                    passTag.setVisible(false);
                    txtPassword.setVisible(false);
                    txtStartBid.setEnabled(true);
                    txtMultiplier.setEnabled(true);
                    txtMinRemaining.setEnabled(true);
                    txtOdds.setEnabled(true);
                    txtMaxBet.setEnabled(true);
                    cmbCurrency.setEnabled(true);
                    btnHigh.setEnabled(true);
                    btnLow.setEnabled(true);
                    btnFloat.setEnabled(true);
                    txtInfo.append("Login successful!\n");
                    txtInfo.setCaretPosition(txtInfo.getText().length());
                }
            }else{
                donate.showdonate();
            }
        }
    }
}

Here is my error:

michaeladair@michaeladair:~/Desktop/dicebot/src$ java dicebotjava.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1086)
    at java.awt.Container.add(Container.java:966)
    at dicebot.<init>(dicebot.java:298)
    at dicebot$1.run(dicebot.java:44)
    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)
michaeladair
  • 163
  • 2
  • 11
  • 4
    Holy code dump Batman! Please learn to use a debugger. – Boris the Spider Jun 17 '14 at 18:24
  • Can you recommend me a good one for Ubuntu please? – michaeladair Jun 17 '14 at 18:25
  • 1
    That depends on your IDE and not your OS. – Boris the Spider Jun 17 '14 at 18:26
  • 1
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – michaeladair Jun 17 '14 at 18:29
  • Flagged my own question for deletion -_- – michaeladair Jun 17 '14 at 18:29
  • 1
    As an aside, none of those component variables should be public and more importantly, none should be static. Consider going through a decent OOPS/Java book to learn the basics of OOP as it applies to Java programs. You won't regret doing this. Also your code has a lot of repetition suggestion a good bit of refactoring will help make your code much more manageable for both you and us. – Hovercraft Full Of Eels Jun 17 '14 at 18:33
  • 1
    Also here: `if(btnLow.getText() == "Roll Low"){`, never use `==` to compare Strings. Use the `.equals(...)` method. Also, you're mixing business logic with view code, something that should be avoided. Consider reading up on MVC (model-view-control) model. – Hovercraft Full Of Eels Jun 17 '14 at 18:39
  • 1
    "Please, do not refer me to another person's question, those don't help because I don't know how it will fix my problem." --> Learning to take in related information and *adapt* it to your situation is an incredibly important skill to have, even outside programming. – awksp Jun 17 '14 at 18:52

2 Answers2

4

The problem is here:

contentPane.add(textPane, BorderLayout.CENTER);

At this point, you haven't set up textPane. You set it up 4 lines later:

textPane = new JTextPane();
textPane.setBackground(Color.DARK_GRAY);
textPane.setEditable(false);
textPane.setMargin(null);
textPane.setContentType("text/html");

You need to add it to the pane after initializing.

Simple debugging would have fixed this problem for you. SO is not an appropriate place for "fix my code" questions.

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
  • 5
    Methinks answering and then saying it's not the place for such questions won't discourage them very effectively. Good job on untangling spaghetti though! – Boris the Spider Jun 17 '14 at 18:25
  • @BoristheSpider Yeah, but this is a Q&A site. Not answering bad questions doesn't really help. If people repeatedly ask bad questions we have systems in place to block them, but as a simple Java programmer I think answering this question causes more good than harm. – Anubian Noob Jun 17 '14 at 18:27
  • @AnubianNoob There's a looooot of discussion on Meta about this kind of thing... Basically comes down to whether you mind noise or not, I think. – awksp Jun 17 '14 at 18:52
  • @user3580294 Well ok bad questions get closed and users get their priviledges revoked, but if I can answer a question then I might as well. – Anubian Noob Jun 17 '14 at 19:05
  • I certainly think that's a noble sentiment. *I* may not mind at the moment, but can't say much about the rest of the community. You're free to check out the discussions on Meta. – awksp Jun 17 '14 at 19:08
  • @user3580294 I'm just a simple man with simple goals. I just want to help :) – Anubian Noob Jun 17 '14 at 19:09
  • Personally, I don't mind helping either. I'm just a little wary of answering "lazy" questions, if that makes sense. – awksp Jun 17 '14 at 19:29
0

Here is what's causing the error. You are adding textPane, before you create it. That's why a null pointer exception is thrown:

contentPane.add(textPane, BorderLayout.CENTER);
txtInfo = new JTextArea("All number formats must use a period(.)\nBot By: MichaelAdair and DalinSprocket\n");
txtInfo.setColumns(35);
txtInfo.setEnabled(false);

textPane = new JTextPane();

Create the textPane object first, then add it.

Edwin Torres
  • 2,774
  • 1
  • 13
  • 15