This site requires JavaScript, please enable it in your browser!
Greenfoot back
GreenfootNewbie2000
GreenfootNewbie2000 wrote ...

2017/6/18

Why do I get a NullPointerException here

Hi, I'm getting a NullPointerException here, because I think everything should be initialised. Thanks for help and fixing the problem
String temp=entry.getKey()+ " " + entry.getValue();
         
                    TEXTFIELD textfield1 =new TEXTFIELD(temp);
                    getWorld().addObject(textfield1,100,200);
danpost danpost

2017/6/18

#
Either 'entry' was not given a value or the actor running the code is not in the world when this code is executed. To determine which, you will need to post the entire class code -- not just the three, or four, lines.
Thanks for your help, that's the full class code:
 public void showHighscore(){
        for(int i=0;i<10;i++){
            for(int k=10;k<getWorld().getHeight();k+=30){
                for (Map.Entry<String, Integer> entry : entriesSortedByValues(highscore)) {
                    // System.out.println(entry.getKey()+ " " + entry.getValue());
                    String temp=entry.getKey()+ " " + entry.getValue();
                    System.out.println(temp);
                    textfields[i]=new TEXTFIELD(temp);
                   
                    getWorld().addObject(new TEXTFIELD("Hallo"),100,200);
                }

            }
        }

    }
And the class TEXTFIELD
public class TEXTFIELD extends Actor
{
 public  TEXTFIELD(String text){
    
    setImage(new GreenfootImage(text,24,Color.BLACK,Color.WHITE));
    
    }
}
I get the NullPointerException when I want to call the getWorld() method Thanks for your help
Super_Hippo Super_Hippo

2017/6/18

#
You did not posted the whole class codes. For the TEXTFIELD class only the import is missing, but the other one is just a method without any information about what class it is or when and how this showHighscore-method is called.
danpost danpost

2017/6/18

#
You are probably removing 'this' from the world before trying to add the TEXTFIELD object into the world (before invoking the 'showHighscore' method).
That's finally the full class code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
import java.io.*;

/**
 * Write a description of class HIGHSCORE here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 * 
 */

public class HIGHSCORE extends Actor
{
    private TreeMap<String,Integer> highscore;
    private TEXTFIELD textfields[];

    public HIGHSCORE(){
        highscore=new TreeMap<String,Integer>();
        textfields=new TEXTFIELD[10];
        readHighscore();

    
    }

    public void showHighscore(){
        for(int i=0;i<10;i++){
            for(int k=10;k<getWorld().getHeight();k+=30){
                for (Map.Entry<String, Integer> entry : entriesSortedByValues(highscore)) {
                    // System.out.println(entry.getKey()+ " " + entry.getValue());
                    String temp=entry.getKey()+ " " + entry.getValue();
                    System.out.println(temp);
                    textfields[i]=new TEXTFIELD(temp);

                    getWorld().addObject(textfields[i],100,k);
                }

            }
        }

    }

    public void writeHighscore(String name,int points){
        highscore.put(name,points);
        try{
            BufferedWriter bw = new BufferedWriter(new FileWriter("highscore.txt"));

            for (Map.Entry<String, Integer> entry : entriesSortedByValues(highscore)){
                String temp=entry.getKey()+ " " + entry.getValue();
                bw.write(temp);
                bw.newLine();
            }
            bw.close();
        }
        catch (IOException e) {
            System.err.println("Error: " + e);
        }

    }

    public void readHighscore(){
        String thisLine;

        //Open the file for reading
        try {
            BufferedReader br = new BufferedReader(new FileReader("highscore.txt"));
            while ((thisLine = br.readLine()) != null) { // while loop begins here

                String[] split = thisLine.split(" ");
                highscore.put(split[0],Integer.parseInt(split[1]));

            } // end while 
            br.close();
        } // end try
        catch (IOException e) {
            System.err.println("Error: " + e);
        }

        for (Map.Entry<String, Integer> entry : entriesSortedByValues(highscore)) {
            System.out.println(entry.getKey()+ " " + entry.getValue());
        }
    }

    private <K,V extends Comparable<? super V>> SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) {
        SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(
                new Comparator<Map.Entry<K,V>>() {
                    @Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
                        int res = e2.getValue().compareTo(e1.getValue());
                        return res != 0 ? res : 1; // Special fix to preserve items with equal values
                    }
                }
            );
        sortedEntries.addAll(map.entrySet());
        return sortedEntries;
    }
}
I hope it helps
danpost danpost

2017/6/18

#
danpost wrote...
You are probably removing 'this' from the world before trying to add the TEXTFIELD object into the world (before invoking the 'showHighscore' method).
From where (show codes) is the 'showHighscore' method called from?
It's called from the Highscore Table class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class HIGHSCORETABLE here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HIGHSCORETABLE extends World
{

    /**
     * Constructor for objects of class HIGHSCORETABLE.
     * 
     */
    HIGHSCORE highscore;
    public HIGHSCORETABLE()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1024, 768, 1); 
        prepare();

    }
    private void prepare() {
        highscore= new HIGHSCORE();
     //  highscore.writeHighscore("Caprisonne",150);
        highscore.showHighscore();

    }

}
danpost danpost

2017/6/19

#
Okay -- so line 25 creates a HIGHSCORE object and line 27 invokes the 'showHighscore' method on that object. However, the method cannot execute properly because it invokes the 'addObject' method on the value returned by calling 'getWorld'. But, 'getWorld' will return 'null' -- not any World object because the HIGHSCORE object is never added into any world. You cannot invoke any method on a 'null' reference.
You need to login to post a reply.