Results 1 to 9 of 9
  1. #1
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default Problem with if statement

    I am having trouble at an if statement within chess game I am designing for java. Basically, when a JButton square is pressed, it takes the piece at that location, followed by a second press that places the piece at that location.

    The method takes the command, parses a few ints from it and passes along to check for the piece at that location. This works just fine without the if statement I added.

    The if statement takes the actionCommand from the button and checks to make sure that the piece being picked up is ok for the current turn. If turn is true, it's black's turn. If false, white's. The string at the square in rules class holds a string. I am grabbing the first letter (B or W) and checking that against turn. On the second click (the piece destination, numberOfClicks == 2) I do not need it to check this however, hence the OR numberOfClicks == 2.

    if(((r.square[ifCheckX][ifCheckY].substring(0,1).equals("B") && r.turn == true) || (r.square[ifCheckX][ifCheckY].substring(0,1).equals("W") && r.turn == false)) || this.numberOfClicks == 2)

    It compiles fine. The first click works fine. The second click throws some nullPointerException, as a result of 2 dozen errors within other classes within java.

    What is wrong with this If statement?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Maybe r or r.square/square[ifCheckX][ifCheckY] is null after the first click!
    Check this with your debuger or simple System.outs before the if statement !

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Java Code:
         if(((r.square[ifCheckX][ifCheckY].substring(0,1).equals("B")     && r.turn == true) 
             || (r.square[ifCheckX][ifCheckY].substring(0,1).equals("W") && r.turn == false)) 
             || this.numberOfClicks == 2)
    What is wrong with this If statement?
    When does it return true or false and you think it should have done the opposite?

  4. #4
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    It doesn't return anything 'cept a HUGE error.

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Gui.actionPerformed(Gui.java:120)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    	at java.awt.Component.processMouseEvent(Component.java:6288)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    	at java.awt.Component.processEvent(Component.java:6053)
    	at java.awt.Container.processEvent(Container.java:2041)
    	at java.awt.Component.dispatchEventImpl(Component.java:4651)
    	at java.awt.Container.dispatchEventImpl(Container.java:2099)
    	at java.awt.Component.dispatchEvent(Component.java:4481)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    	at java.awt.Container.dispatchEventImpl(Container.java:2085)
    	at java.awt.Window.dispatchEventImpl(Window.java:2478)
    	at java.awt.Component.dispatchEvent(Component.java:4481)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
    	at java.awt.EventQueue.access$000(EventQueue.java:84)
    	at java.awt.EventQueue$1.run(EventQueue.java:602)
    	at java.awt.EventQueue$1.run(EventQueue.java:600)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    	at java.awt.EventQueue$2.run(EventQueue.java:616)
    	at java.awt.EventQueue$2.run(EventQueue.java:614)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Last edited by SiX; 07-22-2011 at 06:49 PM.

  5. #5
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    Eek, what is the code tag? >_>;

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Gui.actionPerformed(Gui.java:120)
    With NullPointerExceptions you need to find the variable that is null.
    Look at line 120 in your code and see what variables in it can be null. Then backtrack in your code to find out why that variable does not have a valid value.

    As suggested in post#2 add a println to show the values of ALL the variables used on the line with the error.

  7. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Indeed, whenever you get an error message it tells you what line the error occurred on. This is the best way of figuring out what you did wrong.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  8. #8
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    Could it be throwing the exception if it attempts to substring a NULL? Because the second move is going to a location that is NULL or empty. The check is for either a B or a W in the first letter of the command, for Black or White. I thought I was getting rid of those cases by adding the if(numberOfClicks==2) at the end there...

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    (numberOfClicks==2) at the end
    The checks are made in their order in the statement.
    Move that test to the front to do it first.

    Could be a logic problem if sometimes the contents of a square is null. Have a value for an empty square vs null

    In fact that double array should contain class objects of your design vs Strings. The substring(0,1) is ugly looking and prone to errors.

Similar Threads

  1. Problem with if statement - Char
    By KardKaper in forum New To Java
    Replies: 8
    Last Post: 12-22-2009, 01:14 PM
  2. problem while using return statement
    By shaluchandran in forum New To Java
    Replies: 10
    Last Post: 12-12-2008, 06:29 PM
  3. If-Else statement problem
    By MomenT in forum New To Java
    Replies: 9
    Last Post: 10-23-2008, 08:06 AM
  4. ForLoop statement problem
    By MomenT in forum New To Java
    Replies: 4
    Last Post: 10-01-2008, 01:01 PM
  5. Please help me with this Problem Statement
    By KMS in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 08:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •