-1

i am new to java and as a project i an developing a chess game. i have all the piece moving but when i move the king out of the chess bounds i get the error :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at ChessProject.checkBlackOpponent(chessproject.java:149)
at ChessProject.mouseReleased(chessproject.java:559)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

2 Answers2

0

The else if condition in line 4 to 7 does not check for the absolute value of landingx. Both landingx and landingy must be bigger than 0 and less than 9.

To test the relative position (size of move) a simple condition is enough: else if (Math.abs(startingx-landingx)>1 || Math.abs(startingx-landingx)>1 || ....[test of absolute position])

-1
Here is my code for the king

else if(pieceName.contains("King")) {
   if (((startX - landingX > 1) && (startY == landingY)) || ((startX - landingX > 1) && (startY-landingY > 1)) ||
       ((startX ==landingX) && (startY-landingY > 1)) || ((landingX - startX > 1) && (startY-landingY > 1)) ||
       ((landingX - startX > 1) && (startY==landingY)) || ((landingX - startX > 1) && (landingY - startY > 1)) ||
       ((startX==landingX) && (landingY - startY > 1))|| ((startX-landingX > 1) && (landingY - startY > 1))) {
    validMove = false;
   } else {
    if (piecePresent(e.getX(), (e.getY()))) {
     if (pieceName.contains("White")) {
      if (checkWhiteOpponent(e.getX(), e.getY())) {
       validMove = true;
      } else {  
       validMove = false;
      }
     } else {
      if (checkBlackOpponent(e.getX(), e.getY())) {
       validMove = true;
      } else {
       validMove = false;
      }
     }
    } else {
     validMove = true;
    }
   }
  }
  • Post the code in the question, not in answer – Sergei Sirik Oct 10 '17 at 18:26
  • This should be an edit to your question. It is not an answer. – Fred Larson Oct 10 '17 at 18:27
  • sorry guys i am new to Stack as well. I need to get familiar with it too – Giuseppe Stirpe Oct 10 '17 at 18:28
  • Nothing to do with your problem, but I'd recommend replacing any code of the form `if (condition) result = true; else result = false;` with `result = condition;`. – Fred Larson Oct 10 '17 at 18:31
  • thanks guy and sorry if i've been repetitive. – Giuseppe Stirpe Oct 10 '17 at 18:41
  • Really learn about the great help the Java Runtime gives you with Exceptions: the stacktrace! "java.lang.NullPointerException at ChessProject.checkBlackOpponent(chessproject.java:149)" tells you that in line 149 of chessproject.java some object that shouldn't be null (maybe because you're calling a method on it), is null. Often it's obvious which object that is. If not, set a breakpoint on that line (or on NullPointerException), and look at the variables. – Ralf Kleberhoff Oct 10 '17 at 18:49
  • @Giuseppe Stirpe maybe you can communicate with this user as I would guess you're in the same class https://stackoverflow.com/questions/46569299/ai-chess-java-misunderstanding?noredirect=1#comment80097814_46569299 – achAmháin Oct 10 '17 at 19:35