Nullpointerexception

Carina

Grünschnabel
Hallo ich bin Anfängerin in Java, habe große Probleme und versuche seit 2 Tagen eins zu lösen, komme aber leider nicht zur Lösung ich hoffe ihr könnt mir dabei helfen. Wenn ich das Programm ausführe spuckt mir Eclipse folgende Fehlermeldung:

Exception in thread "main" java.lang.NullPointerException
at de.hohenheim.controller.Fahrplan.getNodeNames(Fahrplan.java:39)
at de.hohenheim.controller.Fahrplan.open(Fahrplan.java:58)
at de.hohenheim.controller.Fahrplan.run(Fahrplan.java:26)
at de.hohenheim.controller.ControllerCanvas$4.handleEvent(ControllerCanvas.java:176)

ich füge mal die jeweiligen Stellen ein:

ControllerCanvas

Code:
public void handleEvent(Event arg0) {
				Fahrplan dialog = new Fahrplan();
				dialog.run();
			}
		});

Fahrplan

Code:
private String[] getNodeNames() {
		Object[] names = map.getNodes().keySet().toArray();
		String[] n = new String[names.length];
		for (int i = 0; i < names.length; i++) {
			n[i] = names[i].toString();
		}
		Arrays.sort(n);
		return n;


System.out.println("Button pressed, about to open modal dialog");
		        final Shell dialogShell = new Shell();
		        dialogShell.setLayout(new FormLayout());
		        {
		        	final Combo node_combo = new Combo(dialogShell, SWT.READ_ONLY);
		    		node_combo.setBounds(5, 45, 150, 25);
		    		String[] items = getNodeNames();
		    		node_combo.setItems(items);
		        }
	}


Ich würde mich riesig auf eine Antwort freuen

LG Carina
 
Code:
private String[] getNodeNames() {
		Object[] names = map.getNodes().keySet().toArray();
		String[] n = new String[names.length];
		for (int i = 1; i < names.length; i++) {
			n[i] = names[i].toString();
		}
		Arrays.sort(n);
		return n;
	}

Hier die 2. zeile
 
Dann gibt es map, map.getNodes(), oder map.getNodes().keySet() nicht.
Ohne mehr Code kann man da schwer etwas sagen.
Was ist map? Sicher, dass es vorher erzeugt wird?
 
Ja das wird in einer anderen Klasse erzeugt, es stellt einen gleisbahn dar auf dem ich später einen Fahrplan simulieren soll. Der Inhalt der Nodemap Klasse sieht wie folgt aus :

Code:
import java.util.ArrayList;
import java.util.HashMap;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;


import de.hohenheim.view.mobile.AnimationFigure;
import de.hohenheim.view.path.PathFigure;
import de.hohenheim.view.node.NodeFigure;

public class NodeMap {
	
	/**
	 * This HashMap Contains all mobile objects, such as trains, ....
	 */
	HashMap<String, AnimationFigure> mobile_objects = new HashMap<String, AnimationFigure>();
	
	/**
	 * This HashMap Contains all paths of all rooms in the NodeMap. The paths are added automatically, when the methods of FigureFactory
	 * are used to create the Figures in the Map.
	 */
	HashMap<NodeFigure, ArrayList<PathFigure>> paths = new HashMap<NodeFigure, ArrayList<PathFigure>>();
	/**
	 * This HashMap Contains all rooms in the NodeMap. The paths are added automatically, when the methods of FigureFactory
	 * are used to create the Figures in the Map.
	 */
	HashMap<String, NodeFigure>  nodes = new HashMap<String, NodeFigure>();
	
	/**
	 * On this figure all rooms are drawn.
	 */
	NodeLayerFigure nodeLayer;
	/**
	 * On this figure all figures which can be animated must be drawn.
	 * It is a transparent layer for the animations.
	 */
	AnimationLayerFigure animationLayer;
		
	/**
	 * 
	 * @return Figure - The Layer where the rooms are drawn onto. 
	 */
	public Figure getNodeLayer() {
		return this.nodeLayer;
	}
	
	/**
	 * 
	 * @return Figure - The Figure where the animations are drawn (AnimationLayerFigure).
	 */
	public Figure getAnimationLayer() {
		return this.animationLayer;
	}
	
	/**
	 * Returns a HasMap with all movable objects inside this RoomMap. 
	 * @return HashMap
	 */
	public HashMap<String, AnimationFigure> getMobileObjects() {
		return this.mobile_objects;
	}
	
	/**
	 * Returns the HashMap which contains all paths of all rooms in this RoomMap.
	 * Key of the {@link HashMap} is the room (RoomFigure). Value is an {@link ArrayList} with all paths (PathFigure)
	 * which are connected to this room.
	 * @return {@link HashMap}
	 */
	public HashMap<NodeFigure, ArrayList<PathFigure>> getPaths() {
		return this.paths;
	}
	/**
	 * Returns the HashMap which contains all rooms in this RoomMap.
	 * Key of the {@link HashMap} is the name of the room as {@link String}. Value is an {@link ArrayList} with all paths ({@link PathFigure})
	 * which are connected to this room.
	 * @return {@link HashMap}
	 */
	public HashMap<String, NodeFigure> getNodes() {
		return this.nodes;
	}
	/**
	 * Constructor to create a new RoomMap	
	 * @param {@link String} roomName - the name of the RoomMap. The name is displayed in the titlebar. 
	 */
	public NodeMap(Canvas c) {
		nodeLayer = new NodeLayerFigure();
	    animationLayer = new AnimationLayerFigure();
	    createNodeContents(c);
	}
	
	/**
	 * Returns the default display. Perhaps we must use another later, 
	 * that is why we let it stay here...
	 * @return {@link Display}
	 */
	public Display getDisplay() {
		return Display.getDefault();
	}
	
	/**O
	 * Initializes the lightweightsystem and the layers for drawing.
	 * @param {@link Canvas} c - the canvas where the lightweight system runs on.  
	 */
	private void createNodeContents(Canvas c) {		
		LightweightSystem lws = new LightweightSystem(c);
		BackgroundLayerdPane layers = new BackgroundLayerdPane();
	    lws.setContents(layers);
	    layers.add(nodeLayer);
	    layers.add(animationLayer);
	}
}
 
Zurück