Results 1 to 6 of 6
  1. #1
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

    Default java.lang.NullPointerException

    Here is the long error message I get when I run the program:
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at projectileProgram.DrawingGraphsThread.<init>(DrawingGraphsThread.java:19)
    	at projectileProgram.ControlPanelJPanel.actionPerformed(ControlPanelJPanel.java:318)
    	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.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(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)
    Im out of ideas of that is causing the error so can someone help?

    Here are the classes with the errors. I've clearly marked where the error occur.
    Java Code:
    package projectileProgram;
    
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ControlPanelJPanel extends JPanel implements ChangeListener, ActionListener, ItemListener{
    	//variables
    	double milliseconds, horizontalVelocity, verticalVelocity, timePassed;
    	long wholeNumber;
    	//Class Objects
    	ProjectileAnimationJPanel projectileAnimationJPanel;
    	TimeSpeedGraphJPanel timeSpeedGraphJPanel;
    	HorizontalVelocityGraphJPanel horizontalVelocityGraphJPanel;
    	VerticalVelocityGraphJPanel verticalVelocityGraphJPanel;
    	ControlPanelJPanel controlPanelJPanel;
    	//setting JSliders
    	JSlider height = new JSlider(0,50,50);
        JSlider angle = new JSlider(0,90,0);
        JSlider speed = new JSlider(0,50,50);
        JButton start, reset;
        JCheckBox timeSpeedGraph, verticalVelocityGraph, horozontalVelocityGraph;
        JLabel heightValues, angleValues, speedValues, time;
        //setting variables elevation, projectionAngle and projectionSpeed
        String heightValue = String.valueOf(height.getValue());
        String angleValue = String.valueOf(angle.getValue());
        String speedValue = String.valueOf(speed.getValue());
        String timePassedString = "0.000";
        //setting JSlider values
        float elevation = Float.parseFloat(heightValue);
    	float projectionAngle = Float.parseFloat(angleValue);
    	float projectionSpeed = Float.parseFloat(speedValue);
        //constructor
    	public ControlPanelJPanel(){
    		//height JSlider
    		height.setMajorTickSpacing(10);
    		height.setMinorTickSpacing(1);
    		height.setPaintTicks(true);
    		height.setPaintLabels(true);
    		height.setOpaque(false);
    		height.addChangeListener(this);
    		
    		//Angle JSlider 
    		angle.setMajorTickSpacing(10);
    		angle.setMinorTickSpacing(1);
    		angle.setPaintTicks(true);
    		angle.setPaintLabels(true);
    		angle.setOpaque(false);
    		angle.addChangeListener(this);
    		
    		//Speed JSlider 
    		speed.setMajorTickSpacing(10);
    		speed.setMinorTickSpacing(1);
    		speed.setPaintTicks(true);
    		speed.setPaintLabels(true);
    		speed.setOpaque(false);
    		speed.addChangeListener(this);
    		
    		//setting time JLabel
    		time = new JLabel("Time/s: "+timePassedString);
    		
    		//creating labels for Sliders	
    		JLabel heightLabel = new JLabel	("Height: ");
            JLabel angleLabel = new JLabel ("Angle: ");
            JLabel speedLabel = new JLabel ("Speed: ");
            
            //creating labels for value of sliders
            heightValues = new JLabel("Height Value: "+heightValue);
            angleValues = new JLabel ("Angle Value: "+angleValue);
            speedValues = new JLabel ("Speed Value: "+speedValue);
            
            //start button
            start = new JButton("start");
            start.setPreferredSize(new Dimension(75,25));
            start.addActionListener(this);
            
            //reset button
            reset = new JButton("reset");
            reset.setPreferredSize(new Dimension(75,25));
            reset.addActionListener(this);
            
            
            //setting JCheckBoxes
            JCheckBox timeSpeedGraph = new JCheckBox("Time Speed Graph");
            timeSpeedGraph.setOpaque(false);
            timeSpeedGraph.addItemListener(this);
            
            JCheckBox verticalVelocityGraph = new JCheckBox("Vertical Velocity Graph");
            verticalVelocityGraph.setOpaque(false);
            verticalVelocityGraph.addItemListener(this);
            
            JCheckBox horozontalVelocityGraph = new JCheckBox("Horozontal Velocity Graph");
            horozontalVelocityGraph.setOpaque(false);
            horozontalVelocityGraph.addItemListener(this);
            
            //setting layout
            this.setLayout(new GridBagLayout());
            GridBagConstraints gc = new GridBagConstraints(); 
            
            //column 0
            gc.weightx = 25;
    		gc.weighty = 100;
            //adding start to screen 
    		gc.gridx = 0;
    		gc.gridy = 1;
    		gc.gridwidth = 1;
    		gc.gridheight = 1;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.NONE;
    		add(start, gc);
    		
    		//adding reset to screen 
    		gc.gridx = 0;
    		gc.gridy = 2;
    		gc.gridwidth = 1;
    		gc.gridheight = 1;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.NONE;
    		add(reset, gc);
    		
    		//adding timePassed
    		gc.gridx = 0;
    		gc.gridy = 6;
    		gc.gridwidth = 1;
    		gc.gridheight = 1;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.NONE;
    		add(time, gc);
            
    		//column 1
    		gc.weightx = 25;
    		gc.weighty = 100;
    		//adding heightLabel to screen 
    		gc.gridx = 1;
    		gc.gridy = 1;
    		gc.gridwidth = 1;
    		gc.gridheight = 1;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.EAST;
    		gc.fill = GridBagConstraints.NONE;
    		add(heightLabel, gc);
    		
    		//column 2
    		gc.weightx = 100;
    		gc.weighty = 100;
    		//adding height to screen
    		gc.gridx = 2;
    		gc.gridy = 0;
    		gc.gridwidth = 1;
    		gc.gridheight = 3;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(height, gc);
    		
    		//adding heightValues to screen
    		gc.gridx = 2;
    		gc.gridy = 3;
    		gc.gridwidth = 2;
    		gc.gridheight = 3;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(heightValues, gc);
    		
    		//column 3
    		gc.weightx = 25;
    		gc.weighty = 100;
    		//adding angleLabel to screen
    		gc.gridx = 3;
    		gc.gridy = 1;
    		gc.gridwidth = 1;
    		gc.gridheight = 1;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.EAST;
    		gc.fill = GridBagConstraints.NONE;
    		add(angleLabel, gc);
    		
    		//column 4
    		gc.weightx = 100;
    		gc.weighty = 100;
    		//adding speed angle to screen
    		gc.gridx = 4;
    		gc.gridy = 0;
    		gc.gridwidth = 1;
    		gc.gridheight = 3;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(angle, gc);
    		
    		//adding angleValues to screen
    		gc.gridx = 4;
    		gc.gridy = 3;
    		gc.gridwidth = 2;
    		gc.gridheight = 3;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(angleValues, gc);
    		
    		//column 5
    		gc.weightx = 25;
    		gc.weighty = 100;
    		//adding speedLabel to screen
    		gc.gridx = 5;
    		gc.gridy = 1;
    		gc.gridwidth = 1;
    		gc.gridheight = 1;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.EAST;
    		gc.fill = GridBagConstraints.NONE;
    		add(speedLabel, gc);
    		
    		//column 6
    		gc.weightx = 100;
    		gc.weighty = 100;
    		//adding speed to screen
    		gc.gridx = 6;
    		gc.gridy = 0;
    		gc.gridwidth = 1;
    		gc.gridheight = 3;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(speed, gc);
    		
    		//adding speedValues to screen
    		gc.gridx = 6;
    		gc.gridy = 3;
    		gc.gridwidth = 2;
    		gc.gridheight = 3;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(speedValues, gc);
    		
    		//column 7
    		gc.weightx = 100;
    		gc.weighty = 100;
    		//adding timeSpeedGraph to screen
    		gc.gridx = 7;
    		gc.gridy = 0;
    		gc.gridwidth = 1;
    		gc.gridheight = 2;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(timeSpeedGraph, gc);
    		
    		//adding horozontalVelocityGraph to screen
    		gc.gridx = 7;
    		gc.gridy = 2;
    		gc.gridwidth = 1;
    		gc.gridheight = 2;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(horozontalVelocityGraph, gc);
    		
    		
    		//adding verticalVelocityGraph to screen
    		gc.gridx = 7;
    		gc.gridy = 4;
    		gc.gridwidth = 1;
    		gc.gridheight = 2;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(verticalVelocityGraph, gc);
    	}
    	//paint method
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		this.setBackground(Color.GREEN);
    	}
    	//returning elevation method
    	public float getElevation(){
    		return elevation;
    	}
    	public float getAngle(){
    		return projectionAngle;
    	}
    	public float getProjectionSpeed(){
    		return projectionSpeed;
    	}
    	//state listener
    	public void stateChanged(ChangeEvent event) {
    		JSlider source = (JSlider) event.getSource();
    		
    		//setting elevation/projectionAngle or projectionSpeed value to new JSlider Value
    		if(source.getValueIsAdjusting() != true){
    			if(event.getSource() == height){
    				heightValue = String.valueOf(height.getValue());
    				elevation = Float.parseFloat(heightValue);
    				heightValues.setText("Height Value: "+heightValue);
    			}else if(event.getSource() == angle){
    				angleValue = String.valueOf(angle.getValue());
    				projectionAngle = Float.parseFloat(angleValue);
    				angleValues.setText("Angle Value: "+angleValue);
    			}else if(event.getSource() == speed){
    				speedValue = String.valueOf(speed.getValue());
    			 	projectionSpeed = Float.parseFloat(speedValue);
    			 	speedValues.setText("Speed Value: "+speedValue);
    			 }
    		}
    	}
    	//action listener
    	public void actionPerformed(ActionEvent event){
    		if(event.getSource().equals(start)){
    			Thread animationThread = new Thread(new AnimationThread(projectileAnimationJPanel, controlPanelJPanel, projectileAnimationJPanel.getElevation(), getAngle(), getProjectionSpeed(), projectileAnimationJPanel.getWidth(), projectileAnimationJPanel.getHeight(), projectileAnimationJPanel.getSizeOfMeter()));
    			animationThread.start();
    
    /////////////////////////////////////////////////////at projectileProgram.ControlPanelJPanel.actionPerformed(ControlPanelJPanel.java:318)//////////////////////////////////////////////////
    			Thread drawingGraphsThread = new Thread(new DrawingGraphsThread(timeSpeedGraphJPanel, horizontalVelocityGraphJPanel, verticalVelocityGraphJPanel, controlPanelJPanel, getAngle(), getProjectionSpeed()));
    			drawingGraphsThread.start();
    			timeSpeedGraphJPanel.settingBooleanRunning(true);
    		}else if(event.getSource().equals(reset)){
    			timePassedString = "0.000";
    			projectileAnimationJPanel.settingXAndYStartingPosition();
    			time.setText("Time/s: "+timePassedString);
    		}
    	}
    	//item listener
    	public void itemStateChanged(ItemEvent event){
    		
    	}
    	//setting values from ProjectileAnimationJPanel
    	public void settingValues(ProjectileAnimationJPanel p, TimeSpeedGraphJPanel t, HorizontalVelocityGraphJPanel hv, VerticalVelocityGraphJPanel vv, ControlPanelJPanel c){
    		projectileAnimationJPanel = p;
    		timeSpeedGraphJPanel = t;
    		horizontalVelocityGraphJPanel = hv;
    		verticalVelocityGraphJPanel = vv;
    		controlPanelJPanel = c;
    	}
    	//setting JLabel time
    	public void settingJLabelTime(double t, double hv, double vv){
    		//setting variables
    		milliseconds = t/1000;
    		horizontalVelocity = hv;
    		verticalVelocity = vv;
    		timePassed = t;
    		
    		//setting whole number
    		if(milliseconds<1)
    			wholeNumber = 0;
    		else
    			wholeNumber = Math.round(Math.floor(milliseconds));
    		
    		timePassedString = String.format("%s.%03d", wholeNumber, Math.round(t)-wholeNumber*1000);
    		time.setText("Time/s: "+timePassedString);
    	}
    	//return time passed since beginning of animation loop
    	public double getTimePassed(){
    		return timePassed;
    	}
    	//return horizontal velocity
    	public double getHoriontalVelocity(){
    		return horizontalVelocity;
    	}
    	//return vertical velocity
    	public double getVerticalVelocity(){
    		return verticalVelocity;
    	}
    }
    Java Code:
    package projectileProgram;
    
    public class DrawingGraphsThread implements Runnable{
    	//variables
    	TimeSpeedGraphJPanel timeSpeedGraphJPanel;
    	HorizontalVelocityGraphJPanel horizontalVelocityGraphJPanel;
    	VerticalVelocityGraphJPanel verticalVelocityGraphJPanel;
    	ControlPanelJPanel controlPanelJPanel;
    	double horizontalVelocity[], verticalVelocity[], timePassed[], speed[], angle;
    	int loopCounter;
    	//constructor
    	public DrawingGraphsThread(TimeSpeedGraphJPanel t, HorizontalVelocityGraphJPanel h, VerticalVelocityGraphJPanel v, ControlPanelJPanel c,float a, float s){
    		//setting variables
    		timeSpeedGraphJPanel = t;
    		horizontalVelocityGraphJPanel = h;
    		verticalVelocityGraphJPanel = v;
    		controlPanelJPanel = c;
    		angle = Double.parseDouble(String.valueOf(a));
    
    ////////////////////////////////////////////at projectileProgram.DrawingGraphsThread.<init>(DrawingGraphsThread.java:19)///////////////////////////////////////////////////
    		speed[0] = Double.parseDouble(String.valueOf(s));
    		horizontalVelocity[0] = s*Math.abs(Math.cos(Math.toRadians(angle)));
    		verticalVelocity[0] = s*Math.sin(Math.toRadians(angle));
    		timePassed[0] = 0;
    		loopCounter = 1;
    	}
    	//run method
    	public void run(){
    		//loop
    		do{
    			try{
    				Thread.sleep(10);
    			}catch(Exception e){};
    			
    			horizontalVelocity[loopCounter] = controlPanelJPanel.getHoriontalVelocity();
    			verticalVelocity[loopCounter] = controlPanelJPanel.getVerticalVelocity();
    			speed[loopCounter] = horizontalVelocity[loopCounter]/Math.abs(Math.cos(Math.toRadians(angle)));
    			timePassed[loopCounter] = controlPanelJPanel.getTimePassed();
    			
    			//setting values in classes
    			timeSpeedGraphJPanel.settingTime(timePassed[loopCounter]);
    			
    			
    			//incrementing loopCounter
    			loopCounter++;
    		}while(timePassed[loopCounter]!=timePassed[loopCounter-1]);
    		timeSpeedGraphJPanel.settingBooleanRunning(false);
    	}
    }
    And here are the rest of the many classes of this program:
    Java Code:
    package projectileProgram;
    
    import javax.swing.JFrame;
    
    public class Run {
    	public static void main(String args[]){
    		ProjectileProgramFixedSize runProgram = new ProjectileProgramFixedSize();
    
    		runProgram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		//setting frame
    		runProgram.setSize(1920,1080);
    		runProgram.setResizable(false);
    		runProgram.setVisible(true);
    	}
    }
    Java Code:
    package projectileProgram;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class ProjectileProgramFixedSize extends JFrame{
    	//class objects
    	ProjectileAnimationJPanel projectileAnimationJPanel = new ProjectileAnimationJPanel();
    	ControlPanelJPanel controlPanelJPanel = new ControlPanelJPanel();
    	TimeSpeedGraphJPanel timeSpeedGraphJPanel = new TimeSpeedGraphJPanel(controlPanelJPanel);
    	HorizontalVelocityGraphJPanel horizontalVelocityGraphJPanel = new HorizontalVelocityGraphJPanel();
    	VerticalVelocityGraphJPanel verticalVelocityGraphJPanel = new VerticalVelocityGraphJPanel();
    	//variables
    	JScrollPane timeSpeedGraphJScrollPane = new JScrollPane(timeSpeedGraphJPanel);
    	JScrollPane horizontalVelocityGraphJScrollPane = new JScrollPane(horizontalVelocityGraphJPanel);
    	JScrollPane verticalVelocityGraphJScrollPane = new JScrollPane(verticalVelocityGraphJPanel);
    	
    	//constructor
    	public ProjectileProgramFixedSize(){
    		super("Projectile Program");
    		setLayout(new GridBagLayout());
    		
    		//setting layout
    		GridBagConstraints gc = new GridBagConstraints();
    		gc.gridx = 0;
    		gc.gridy = 0;
    		gc.gridwidth = 1;
    		gc.gridheight = 3;
    		gc.weightx = 100;
    		gc.weighty = 100;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		this.add(projectileAnimationJPanel, gc);
    		
    		gc.gridx = 1;
    		gc.gridy = 0;
    		gc.gridwidth = 1;
    		gc.gridheight = 1;
    		gc.weightx = 50;
    		gc.weighty = 50;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		this.add(timeSpeedGraphJScrollPane, gc);
    		
    		gc.gridx = 1;
    		gc.gridy = 1;
    		gc.gridwidth = 1;
    		gc.gridheight = 1;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		this.add(horizontalVelocityGraphJScrollPane, gc);
    
    		gc.gridx = 1;
    		gc.gridy = 2;
    		gc.gridwidth = 1;
    		gc.gridheight = 1;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		this.add(verticalVelocityGraphJScrollPane, gc);
    		
    		gc.gridx = 0;
    		gc.gridy = 3;
    		gc.gridwidth = 2;
    		gc.gridheight = 1;
    		gc.weighty = 5;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		this.add(controlPanelJPanel, gc);
    		
    		settingVariablesForOtherClasses();
    	}
    	public void settingVariablesForOtherClasses(){
    		//creating thread to set elevation
    		Thread settingElevationThread = new Thread(new SettingElevationThread(controlPanelJPanel, projectileAnimationJPanel));
    		settingElevationThread.start();
    		
    		//setting x and y positions in ProjectileAnimationJPanel class on first run
    		projectileAnimationJPanel.settingXAndYPosisions(projectileAnimationJPanel.getWidth()/20-projectileAnimationJPanel.getHeight()/40, projectileAnimationJPanel.getHeight()-controlPanelJPanel.getElevation()-projectileAnimationJPanel.getHeight()/8-projectileAnimationJPanel.getHeight()/20);
    		
    		//giving actionPerformed() in ControlPanelJPanel class the variables needed to start and run threads
    		controlPanelJPanel.settingValues(projectileAnimationJPanel, timeSpeedGraphJPanel, horizontalVelocityGraphJPanel, verticalVelocityGraphJPanel, controlPanelJPanel);
    		
    		//starting repaint thread
    		Thread repaintThread = new Thread(new RepaintThread(projectileAnimationJPanel, timeSpeedGraphJPanel, horizontalVelocityGraphJPanel, verticalVelocityGraphJPanel, controlPanelJPanel));
    		repaintThread.start();
    	}
    }
    Java Code:
    package projectileProgram;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class ProjectileAnimationJPanel extends JPanel{
    	//variables
    	float elevationDP, meter;
    	int elevation, previousElevation, xPosition, yPosition;
    	int i = 0;
    	boolean run = false;
    	//paint method
    	public void paintComponent(Graphics g){
    		Graphics2D g2 = (Graphics2D)g;
    		super.paintComponent(g2);
    		this.setBackground(Color.BLUE);
    		
    		//adding anti-aliasing
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    		
    		//drawing elevation
    		g2.setColor(Color.GREEN);
    		g2.fillRect(0, getHeight()-elevation-(this.getHeight()/8),this.getWidth()/20 , elevation);
    		
    		//drawing ground
    		g2.setColor(Color.GREEN);
    		g2.fillRect(0, 7*getHeight()/8, getWidth(), getHeight()/8+1);
    		
    		//drawing ball
    		g2.setColor(Color.RED);
    		g2.fillOval(xPosition, yPosition, getHeight()/20, getHeight()/20);
    				
    		//drawing black lines
    		g2.setColor(Color.BLACK);
    		//drawing on ground
    		g2.drawLine(getWidth()/20, 7*getHeight()/8, getWidth(), 7*getHeight()/8);
    		//drawing line on top of elevation
    		g2.drawLine(0, 7*getHeight()/8-elevation, getWidth()/20, 7*getHeight()/8-elevation);
    		//drawing line at side of elevation
    		g2.drawLine(getWidth()/20, 7*getHeight()/8-elevation, getWidth()/20, 7*getHeight()/8);
    	}
    	public void setElevation(float e){
    		elevationDP = (getWidth()*(e/50))/8;
    		elevation = Math.round(elevationDP);
    		
    		//testing for change in elevation
    		if(previousElevation != elevation){
    			//setting height ball to new elevation
    			settingXAndYStartingPosition();
    			
    			//setting previousElevation to new elevation
    			previousElevation = elevation;
    		}
    	}
    	public float getSizeOfMeter(){
    		meter = Float.parseFloat(String.valueOf((getWidth())));
    		return meter/400;
    	}
    	public float getElevation(){
    		return elevation;
    	}
    	public void settingXAndYPosisions(double x, double y){
    		xPosition = Math.round(Float.parseFloat(String.valueOf(x)));
    		yPosition = Math.round(Float.parseFloat(String.valueOf(y)));
    	}
    	public void settingXAndYStartingPosition(){
    		xPosition = getWidth()/20-getHeight()/40;
    		yPosition = 7*getHeight()/8-elevation-getHeight()/20;
    	}
    }
    Java Code:
    package projectileProgram;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class TimeSpeedGraphJPanel extends JPanel{
    	//variables 
    	int[] speed, time;
    	int counter = 0;
    	boolean running = false;
    	ControlPanelJPanel controlPanelJPanel;
    	//constructor
    	public TimeSpeedGraphJPanel(ControlPanelJPanel c){
    		controlPanelJPanel = c;
    	}
    	//paint method
    	public void paintComponent(Graphics g){
    		//housekeeping
    		super.paintComponent(g);
    		this.setBackground(Color.WHITE);
    		Graphics2D g2 = (Graphics2D)g;
    		//variables
    		int counterVScale = 10;
    		int counterHScale = 5;
    		
    		g2.setColor(Color.LIGHT_GRAY);
    
    		//vertical lines
    		for(int i = 30;i < getWidth(); i += 10){
    			g2.drawLine(i, 0, i, getHeight()-25);
    			
    			//x axis values
    			if(counterVScale == 10){
    				g2.drawString(String.valueOf(i/100), i-3, getHeight()-12);
    				counterVScale = 0;
    			}
    			counterVScale++;
    		}
    		//horizontal lines
    		for(int i = 25;i < getHeight(); i += 10){
    			g2.drawLine(30, getHeight()-i, getWidth(), getHeight()-i);
    			
    			//y axis values
    			if(counterHScale == 5){
    				g2.drawString(String.valueOf(i/5-5), 15, getHeight()-i+5);
    				counterHScale = 0;
    			}
    			counterHScale++;
    		}
    		
    		//drawing x axis label
    		g2.drawString("Time/s", getWidth()/2-15, getHeight()-1);
    		//drawing y axis label
    		g2.rotate(Math.toRadians(90), 1, getHeight()/2);
    		g2.drawString("Speed m/s", -25, getHeight()/2);	
    		
    		//drawing line graph
    		if(running == true){
    			for(int c = 0; c<= time.length; c++){
    				g.drawLine(time[counter-1], speed[counter-1], time[counter], speed[counter]);
    			};
    		}
    	}
    	public void settingTime(double t){
    		time[counter] = Math.round(Float.parseFloat(String.valueOf(t)));
    	}
    	public void settingSpeed(double s){
    		speed[counter] = Math.round(Float.parseFloat(String.valueOf(s)));
    	}
    	public void settingBooleanRunning(boolean r){
    		running = r;
    	}
    }
    Java Code:
    package projectileProgram;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class HorizontalVelocityGraphJPanel extends JPanel{
    	//paint method
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		this.setBackground(Color.WHITE);
    		Graphics2D g2 = (Graphics2D)g;
    		//variables
    		int counterVScale = 10;
    		int counterHScale = 5;
    		
    		g2.setColor(Color.LIGHT_GRAY);
    
    		//vertical lines
    		for(int i = 30;i < getWidth(); i += 10){
    			g2.drawLine(i, 0, i, getHeight()-25);
    			
    			//x axis values
    			if(counterVScale == 10){
    				g2.drawString(String.valueOf(i/100), i-3, getHeight()-12);
    				counterVScale = 0;
    			}
    			counterVScale++;
    		}
    		//horizontal lines
    		for(int i = 25;i < getHeight(); i += 10){
    			g2.drawLine(30, getHeight()-i, getWidth(), getHeight()-i);
    			
    			//y axis values
    			if(counterHScale == 5){
    				g2.drawString(String.valueOf(i/5-5), 15, getHeight()-i+5);
    				counterHScale = 0;
    			}
    			counterHScale++;
    		}
    		
    		//drawing x axis label
    		g2.drawString("Time/s", getWidth()/2-15, getHeight()-1);
    		//drawing y axis label
    		g2.rotate(Math.toRadians(90), 1, getHeight()/2);
    		g2.drawString("Horizontal Speed m/s", -60, getHeight()/2);
    	}
    }
    Java Code:
    package projectileProgram;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class VerticalVelocityGraphJPanel extends JPanel{
    	//paint method
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		this.setBackground(Color.WHITE);
    		Graphics2D g2 = (Graphics2D)g;
    		//variables
    		int counterVScale = 10;
    		int counterHScale = 5;
    		
    		g2.setColor(Color.LIGHT_GRAY);
    
    		//vertical lines
    		for(int i = 30;i < getWidth(); i += 10){
    			g2.drawLine(i, 0, i, getHeight()-25);
    			
    			//x axis values
    			if(counterVScale == 10){
    				g2.drawString(String.valueOf(i/100), i-3, getHeight()-12);
    				counterVScale = 0;
    			}
    			counterVScale++;
    		}
    		//horizontal lines
    		for(int i = 25;i < getHeight(); i += 10){
    			g2.drawLine(30, getHeight()-i, getWidth(), getHeight()-i);
    			
    			//y axis values
    			if(counterHScale == 5){
    				g2.drawString(String.valueOf(i/5-5), 15, getHeight()-i+5);
    				counterHScale = 0;
    			}
    			counterHScale++;
    		}
    		
    		//drawing x axis label
    		g2.drawString("Time/s", getWidth()/2-15, getHeight()-1);
    		//drawing y axis label
    		g2.rotate(Math.toRadians(90), 1, getHeight()/2);
    		g2.drawString("Vertical Speed m/s", -50, getHeight()/2);	
    	}
    	public void setPointsMethod(double x, double y, int c){
    		
    	}
    }
    Java Code:
    package projectileProgram;
    
    import javax.swing.JPanel;
    
    public class AnimationThread implements Runnable{
    	//variables
    	ProjectileAnimationJPanel projectileAnimationJPanel;
    	ControlPanelJPanel controlPanelJPanel;
    	JPanel panel = new JPanel();
    	double elevation, projectionAngle, projectionSpeed,  xVelocity, yVelocity, xPosition, yPosition, height, width, startTime, timeOfLastRun, timeBetweenRuns, meter;
    	double timePassed = 0;
    	//constructor 
    	public AnimationThread(ProjectileAnimationJPanel p, ControlPanelJPanel c, float e, float a, float s, int w, int h, float m){	
    		projectileAnimationJPanel = p;
    		controlPanelJPanel = c;
    		
    		//converting numbers to strings then to doubles
    		meter = Double.parseDouble(String.valueOf(m));
    		elevation = Double.parseDouble(String.valueOf(e));
    		projectionAngle = Double.parseDouble(String.valueOf(a));
    		projectionSpeed = Double.parseDouble(String.valueOf(s))*meter;
    		width = Double.parseDouble(String.valueOf(w));
    		height = Double.parseDouble(String.valueOf(h));
    		
    		//finding horizontal and vertical velocities
    		xVelocity = projectionSpeed*Math.abs(Math.cos(Math.toRadians(projectionAngle)));
    		yVelocity = -projectionSpeed*Math.sin(Math.toRadians(projectionAngle));
    		
    		//setting starting positions
    		xPosition = width/20-height/40;
    		yPosition = 7*height/8-elevation-height/20;
    	}
    	public void run(){
    		//setting startTime
    		startTime = System.currentTimeMillis();	
    		
    		//loop
    		do{
    			//making thread sleep
    			try{
    				Thread.sleep(1);
    			}catch(Exception e){}
    			
    			//setting time
    			timeOfLastRun = timePassed;
    			timePassed = System.currentTimeMillis()- startTime;
    			timeBetweenRuns = timePassed - timeOfLastRun;
    			
    			//changing position
    			xPosition += xVelocity*timeBetweenRuns/1000;
    			yVelocity += 9.8*meter*timeBetweenRuns/1000;
    			yPosition += yVelocity*timeBetweenRuns/1000;
    			
    			//setting x and y positions in ProjectileAnimationJPanel class
    			projectileAnimationJPanel.settingXAndYPosisions(xPosition, yPosition);
    			//setting time JLabel in controlPanel class
    			controlPanelJPanel.settingJLabelTime(timePassed, xVelocity, yVelocity);
    		}while(yPosition<7*height/8-height/20);
    	}
    }
    Java Code:
    package projectileProgram;
    
    public class RepaintThread implements Runnable{
    	//variables
    	ProjectileAnimationJPanel projectileAnimationJPanel;
    	TimeSpeedGraphJPanel timeSpeedGraphJPanel;
    	HorizontalVelocityGraphJPanel horizontalVelocityGraphJPanel;
    	VerticalVelocityGraphJPanel verticalVelocityGraphJPanel;
    	ControlPanelJPanel controlPanelJPanel;
    	//constructor
    	public RepaintThread(ProjectileAnimationJPanel p, TimeSpeedGraphJPanel t, HorizontalVelocityGraphJPanel h, VerticalVelocityGraphJPanel v, ControlPanelJPanel c){
    		projectileAnimationJPanel = p;
    		timeSpeedGraphJPanel = t;
    		horizontalVelocityGraphJPanel = h;
    		verticalVelocityGraphJPanel = v;
    		controlPanelJPanel = c;
    	}
    	//run method
    	public void run(){
    		for(int i = 0;i >= 0; i++){
    			//making thread sleep
    			try{
    				Thread.sleep(10);
    			}catch(Exception e){}
    			
    			//calling repaint methods
    			projectileAnimationJPanel.repaint();
    			timeSpeedGraphJPanel.repaint();
    			horizontalVelocityGraphJPanel.repaint();
    			verticalVelocityGraphJPanel.repaint();
    		}
    	}
    }
    Last edited by peterhabe; 08-27-2010 at 08:22 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at projectileProgram.DrawingGraphsThread.<init>(Drawi ngGraphsThread.java:19)
    Look at line 19 in DrawingGraphsThread.
    What variable on that line is null? Then track back thru your code to see why it is null.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    Look at line 19 in DrawingGraphsThread.
    What variable on that line is null? Then track back thru your code to see why it is null.
    My guess: the speed[] array is null. (and there are more things not initialized).

    kind regards,

    Jos

  4. #4
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

    Default

    I've tried but if I put
    Java Code:
    System.out.println(Double.parseDouble(String.valueOf(s)));
    , which is what the value on line 19 been set to, on the line before it comes out with a value.

  5. #5
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

    Default

    JosAH looks like you guessed right.:p cheers
    Last edited by peterhabe; 08-27-2010 at 08:42 PM.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by peterhabe View Post
    JosAH looks like you guessed right.:p cheers
    1) I'm always right.
    2) unless I'm wrong but then rule 1) applies.

    kind regards,

    Jos ;-)

Similar Threads

  1. java.lang.nullPointerException
    By KSUliz in forum New To Java
    Replies: 10
    Last Post: 04-11-2010, 07:15 PM
  2. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 3
    Last Post: 02-28-2009, 05:41 AM
  3. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 12:36 PM
  4. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 09:01 AM
  5. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 06:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •