0

I am not sure why I am getting this error can anyone help

int count=0;
        positions curr=new positions(0,0);
        
        while(count!=64) {
            positions temp=null;
            for(int i=0; i<p.length; i++) {
                for(int j=0; j<p[0].length; j++) {
                    if(p[i][j].equals(curr)) {
                        
                        
                        while(temp.isVisited()!=true) {
                            ArrayList<positions> k =curr.getpositions(p, curr);
                            
                            temp=k.get(0);
                            for(int l=1; l<k.size(); l++) {
                                if(k.get(l).getMoves()<temp.getMoves()) {
                                    temp=k.get(l);
                                }
                            }
                            
                        }
                        
                    }
                    
                }
            }
            curr=temp;
            solution[curr.getRow()][curr.getCol()]=count;
            count++;
            
        }

my error is Exception in thread "main" java.lang.NullPointerException: Cannot invoke "positions.getRow()" because "curr" is null at Main.main(Main.java:41)

  • You're also going to get a `NullPointerException` at the `while (temp.isVisited() == true)` because `temp` is never assigned a non-null value prior to that call. I suspect in your case that the `for` loop isn't even running, though, and so when `curr = temp;` runs, you're effectively running `curr = null;` because `temp` never gets set. – fireshadow52 Jun 10 '22 at 03:51
  • what should I set temp to be equal to then? – coolcatpanther Jun 10 '22 at 03:59
  • I'm not sure. But you could turn the `while` loop into a `do-while` loop; that way `temp` will actually have a value when the condition is checked. – fireshadow52 Jun 10 '22 at 04:44

0 Answers0