Hi,
bekomme das MVC Beispiel einfach nicht zum laufen, benutze eclipse 3.1.1 mit java 1.5, enum erkennt er.
Habe die importe ergänzt sowie ein Panel im WindViewer erzeugt, das Programm startet zeigt aber nur 2 Knöpfe mit den Aufschriften: Change Direction, Change Speed
Was übersehe ich?
Die Fehlermeldung nach dem betätigen von Change Direction:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at test.view.WindViewer.update(WindViewer.java:68)
at java.util.Observable.notifyObservers(Unknown Source)
at test.controll.WindController.changeDirection(WindController.java:30)
at test.view.WindViewer$1.actionPerformed(WindViewer.java:46)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Die Fehlermeldung nach dem betätigen von Change Speed:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at test.view.WindViewer.update(WindViewer.java:68)
at java.util.Observable.notifyObservers(Unknown Source)
at test.controll.WindController.changeSpeed(WindController.java:36)
at test.view.WindViewer$2.actionPerformed(WindViewer.java:54)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Julius
bekomme das MVC Beispiel einfach nicht zum laufen, benutze eclipse 3.1.1 mit java 1.5, enum erkennt er.
Habe die importe ergänzt sowie ein Panel im WindViewer erzeugt, das Programm startet zeigt aber nur 2 Knöpfe mit den Aufschriften: Change Direction, Change Speed
Was übersehe ich?
Die Fehlermeldung nach dem betätigen von Change Direction:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at test.view.WindViewer.update(WindViewer.java:68)
at java.util.Observable.notifyObservers(Unknown Source)
at test.controll.WindController.changeDirection(WindController.java:30)
at test.view.WindViewer$1.actionPerformed(WindViewer.java:46)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Die Fehlermeldung nach dem betätigen von Change Speed:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at test.view.WindViewer.update(WindViewer.java:68)
at java.util.Observable.notifyObservers(Unknown Source)
at test.controll.WindController.changeSpeed(WindController.java:36)
at test.view.WindViewer$2.actionPerformed(WindViewer.java:54)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Code:
package test.controll;
/**
* @author deathbyaclown
*/
public interface WindControllable {
public void changeSpeed();
public void changeDirection();
}
package test.controll;
import test.model.*;
import test.view.*;
import java.util.*;
/**
* @author deathbyaclown
*/
public class WindController extends Observable implements WindControllable {
private Wind wind;
public WindController() {
WindViewer viewer = new WindViewer( this );
addObserver( viewer );
wind = new Wind();
}
public void changeDirection() {
Direction[] dir = Direction.values();
wind.setDir( dir[ ( int ) ( Math.random() * 4 ) ] );
setChanged();
notifyObservers( wind );
}
public void changeSpeed() {
wind.setSpeed( ( int ) ( Math.random() * 100 ) );
setChanged();
notifyObservers( wind );
}
public static void main(String[] args) {
new WindController();
}
}
package test.model;
/**
* @author deathbyaclown
*/
public enum Direction {
NORTH, EAST, SOUTH, WEST
}
package test.model;
/**
* @author deathbyaclown
*/
public class Wind {
private Direction dir = Direction.NORTH;
private int speed = 0;
/**
* @return Returns the dir.
*/
public Direction getDir() {
return dir;
}
/**
* @param dir The dir to set.
*/
public void setDir(Direction dir) {
this.dir = dir;
}
/**
* @return Returns the speed.
*/
public int getSpeed() {
return speed;
}
/**
* @param speed The speed to set.
*/
public void setSpeed(int speed) {
this.speed = speed;
}
}
package test.view;
import test.model.*;
import test.controll.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
* @author deathbyaclown
*/
public class WindViewer extends JFrame implements Observer {
private WindControllable controller;
private JLabel direction;
private JLabel speed;
public WindViewer( WindControllable controller ) {
super( "WindoController" );
this.controller = controller;
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
init();
pack();
setVisible( true );
}
private void init() {
// code für die elemente wurde weggelassen
//Panel erstellen
JPanel buttonPanel = new JPanel(); // hinzugefügt ---------------------------------------------------------------------------------
JButton button = new JButton( "Change Direction" );
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
controller.changeDirection();
}
} );
buttonPanel.add( button );
button = new JButton( "Change Speed" );
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
controller.changeSpeed();
}
} );
buttonPanel.add( button );
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
}
/*
* (non-Javadoc)
*
* @see java.util.Observer#update(java.util.Observable, java.lang.Object)
*/
public void update(Observable arg0, Object arg1) {
Wind wind = ( Wind ) arg1;
direction.setText( wind.getDir().toString() );
speed.setText( String.valueOf( wind.getSpeed() ) );
}
}
Julius