0

Say for example, if in a program you get a null-pointer error for adding a piece of code to your which program makes the program run fine but without that piece of code the program doesn't work as expected, would it be a good idea to allow the null-pointer error to happen, if so, is there any way of catching it before it displays onto the console. 1 way I am aware of is, using try and catch but in my past experience this hasn't worked, my attempt at using this might be wrong, but this is how I tried it.

try {
    // line / s of code to catch the error from

} catch (java.lang.NullPointerException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

edited: The list of error i am getting:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at playAGame.endGameResult(playAGame.java:204)
    at playAGame.checkingWinner(playAGame.java:159)
    at playAGame.callCheckWinner(playAGame.java:179)
    at playAGame.moveSetup(playAGame.java:66)
    at playAGame$1.actionPerformed(playAGame.java:52)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

this is because of this line: button = new JButton[button.length][button.length];

I am creating a TicTacToe game and if I remove that line from my code, the game won't work properly.

edited: I believe this is one of the lines providing the null pointer, correct me if I am wrong. Basically, I have created a method checking if three given buttons have the value X, if it does then trigger the win variable to true. This is how I am checking if someone has won the TicTacToe game.

public void checkingWinner(JButton button1, JButton button2, JButton button3) { 

        if(buttonA.getText().equals(buttonB.getText()) && buttonB.getText().equals(buttonC.getText()) && buttonA.getText().equals("X"))
        { 
win = true;
System.out.pl("winner is X");   


    }
user3449793
  • 21
  • 1
  • 3
  • 2
    No, you should never catch a NullPointerException. A NPE is used by the runtime to signal a bug in your code. Catching the exception would only hide the bug. You should instead read the stack trace of the exception to know where the exception comes from, and fix the code to avoid it. Of course, try/catch work fine. It's impossible to tell you waht youdid wrong with only the code you posted. – JB Nizet Mar 23 '14 at 14:23
  • Show your code with stack trace... – Suzon Mar 23 '14 at 14:25
  • "would it be a good idea to allow the null-pointer error to happen". I don't understand what you mean to "allow" an error. Exceptions are intended to be managed or thrown. And maybe logged. That's it. – aliteralmind Mar 23 '14 at 14:25
  • possible duplicate of [What is a Null Pointer Exception?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) – donfuxx Mar 23 '14 at 14:42
  • edited post with the error i am getting. – user3449793 Mar 23 '14 at 14:45
  • The only thing that can cause this exception at the given line is that `button` is null. Make sure it isn't. – JB Nizet Mar 23 '14 at 15:00
  • The buttons are represented as buttons[3][3] as global variables. basically, by the line of code above, i am trying to reset the game to its start position, which it does but it gives the null pointer error. – user3449793 Mar 23 '14 at 15:07
  • We can't find and explain a bug in your code without seeing your code. – JB Nizet Mar 23 '14 at 15:08

2 Answers2

0

It's hard to tell without seeing the whole code, but it might be the case that the initialization you're doing of the array is throwing an exception since it's the first time it's being initialized, during which time it is being referenced.

If that's the case, you should solve this by using a constant for the width and height instead of self reference:

public static final int HEIGHT = ...;
public static final int WIDTH = ...;
...
button = new JButton[HEIGHT][WIDTH];
ethanfar
  • 3,733
  • 24
  • 43
  • This line of code is in my gameRestart method and basically by this what I am trying to is set the value of all the buttons to null for the next game, otherwise the give carries on some information from the previous game. I have previously created these button outside my main class. – user3449793 Mar 23 '14 at 15:03
  • If that's the line of code the exception is coming from, the only thing that can go wrong there is the access to button (i.e. button.length). Could you share some more code ? It would be much easier to help you that way – ethanfar Mar 23 '14 at 15:18
  • Hi, sorry for the late reply, I have edited the post and I believe that is one of the lines giving the null pointer. – user3449793 Mar 23 '14 at 15:46
  • Are you sure your code's latest version is compiled ? The stack trace doesn't make a lot of sense given the code you're showing. – ethanfar Mar 23 '14 at 16:09
  • I am using eclipse, I know what the error is but don't know how to solve it. Can we take this over to chat? So i can explain it better? – user3449793 Mar 23 '14 at 16:14
  • grrr, I need 20 reputation to chat in the chatroom. – user3449793 Mar 23 '14 at 16:42
  • The easiest way to go about solving your problem, is if you post the entire playAGame class's source code (with line numbering if possible). I'm sure any one of us could help you solve your problem fairly quickly if you do. – ethanfar Mar 23 '14 at 17:13
-2

Are you sure that the line/s of code are throwing a NullPointerException? Because it works for me.

public class Main {
    static String a;
    public static void main(String args[]) {
        try {
            a.charAt(0);
        } catch (java.lang.NullPointerException e) {
            System.out.println("Thrown");
            e.printStackTrace();
        }
    }
}
punkle
  • 952
  • 1
  • 8
  • 17