-5

I am trying to write the game 2048 in java, right now I only make one row to understand how it works. Using HashMap and using Collection.shuffle method to get the random Point position. When I run my code, it places the random number in a random spot, but I also got the syntax error, "Exception in thread "main" java.lang.NullPointerException" and it shows the problems are in my Model class my getValue(int row, int column) notificationToView() What is the error referring to? How can I fix it?

import java.util.Random;
import java.awt.Point;
import java.util.Collections;
import java.util.HashMap;
import java.util.ArrayList;


public class Model  {

    View _view;

private HashMap<Point, Integer> _map;

    public Model(){
        _map = new HashMap<Point, Integer>();

        for(int i = 0; i < 4; i++)
            _map.put(new Point(0,i), -1);


}


/**
 * Assigns randomly generated int values to the 
 * variables of the model.
 */
public void notificationToView() {

        _map.put(getRandomPoint(), getRandom2OR4());

    _view.updateView();
}



public int getRandom2OR4(){
    Random r = new Random();

    if(r.nextInt() % 5 == 0 )    
        return 4;
    else
        return 2;               
}

public Point getRandomPoint(){  
    ArrayList<Point> unoccupied = new ArrayList<Point>();

    for (int i = 0; i < 4; i++)
    {
        if(_map.get(new Point(0,i)) == -1)
            unoccupied.add(new Point(0,i));
    }

    Collections.shuffle(unoccupied);
    Point random = unoccupied.get(0);

     return random;
}

public int getValue( int row, int column) {
    return  _map.get(new Point(row, column));
}


/**
 * Method that associates the a view as an observer
 * of the model.  Being an observer implies receiving
 * notifications when the data in the model changes.
 */
public void addObserver(View view) {
    _view = view;
}

}




public class View implements Runnable {
    private JFrame _frame;
    private JPanel _panel;
    private Model _model;
    private ArrayList<JButton> _list;

    public View(Model m){
        _model = m;


    }

  /**
   * This method updates the view based on the current
   * values stored in the data model.
   */
   public void updateView() {

        for(int i = 0; i < 4; i++){
            if(_model.getValue(0,i) != -1)
                _list.get(i).setText("" + _model.getValue(0, i));
            else 
                _list.get(i).setText("");
    }
    }

    @Override
    public void run() {

        _list = new ArrayList<JButton>();
        _frame = new JFrame("Lab7");
        _panel = new JPanel();

       // create 4 buttons

        for(int i = 0; i < 4; i++){

            JButton buttons = new JButton();
            _list.add(buttons);   

            _panel.add(_list.get(i));

            _list.get(i).setFont(new Font("Georgia", Font.PLAIN,40));
            _list.get(i).addKeyListener(new MyKeyEvent(_model)); 

}

mandy
  • 1
  • From exception stack trace, navigate to the exact line where its appearing - that is the line with null pointer - check for an uninitialized variable and probably you need to initialize it or do a null check before operation. – Pavan Kumar Apr 17 '15 at 07:58
  • exception is not an syntax error,... syntax error prohibits you from compiling the code so it never can run,... Exception is thrown when an error occur during runtime in your case I suspect you are using pointer that is NULL which means you are accessing memory at adress 00000000:00000000 which is reserved for system and you do not have access rights to it ... my bet is you are using `_map` prior to allocation or the getValue is called for NULL object – Spektre Apr 17 '15 at 08:01
  • Generally Null Pointer exception happens when a variable is not initialized and then accessed. Compilers usually interpret this can happen can shows error where applicable. Check the value of variables . Make sure they have a value before they are used in pgm. Initialize them at declaration if necessary. – Raj Apr 17 '15 at 08:11

1 Answers1

0

In method

public int getValue( int row, int column)

when you do return _map.get(new Point(row, column)); you're asking the map to get element having key :

new Point(row, column)

So the map look inside its keys, and call method .equals to know if the key you provide is the same.

So it's seems you call the method with some row / column values that are not existing in the map

No matching element is found in map and return null.

As your map contains Integer class, and you method signature return an int, outoboxing take place and create a nullPointerException, try to convert null to an int.

So try to replace :

return  _map.get(new Point(row, column));

by

Integer currentValue = _map.get(new Point(row, column));
if (currentValue != null){
  return currentValue
}
return -1;
Vyncent
  • 1,185
  • 7
  • 17