Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-30-2008, 02:15 PM
Member
 
Join Date: Apr 2008
Posts: 1
rohan is on a distinguished road
Null pointer Exception. after a bit of execution!! Plz help me
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.border.BevelBorder;

import javax.swing.Timer;
import javax.swing.Timer;

class CA_1D2S3N {

private byte[][]rows;

private int curRow=0,nextRow=1;
private int rowSize=50;
private int cellStates=2;

private byte[] rules={0,1,1,1,1,0,0,0};

public CA_1D2S3N(){}

public CA_1D2S3N(int rSize){

rowSize=rSize;
rows=new byte[2][];
rows[curRow] =new byte[rowSize];
rows[nextRow] =new byte[rowSize];

reset();
}
public int getRowSize(){
return rowSize;
}
public void reset(){
for(int i=curRow;i<=nextRow;++i){
for(int j=0;j<rowSize;j++){
rows[i][j]=0;
}
}
rows[curRow][(int)(rowSize/2)]=1;


}
public void TimeStep(){
for(int i=1;i<rowSize-2;++i){
int state=Map(rows[curRow],i);

rows[1][i]=rules[state];
}
for(int i=0;i<rowSize;i++){
rows[0][i]=rows[1][i];

}

}

int Map(byte []row,int i){

int x=4*row[i-1] + 2*row[i] + 1*row[i+1];

return x;
}
byte[] getcurrentRow(){

return rows[curRow].clone();
}
}


public class MainFrame extends JApplet implements ActionListener{

public JPanel pane;
CA_1D2S3N ca=new CA_1D2S3N (50);
int timeStep=101;
public JMenuBar menuBar;

public JToolBar toolBar;
JButton Tstart;
JButton Tstop;

public static Timer timer=new Timer(100,null);


public void timerEvent(ActionEvent e){

ca.TimeStep();

ShowCurrentRow(ca.getcurrentRow(),++timeStep);
if (timeStep>100) timer.stop();

}



public MainFrame() {
menuBar = new JMenuBar();

JMenu formatMenu = new JMenu("Timer");
formatMenu.add("Start");
formatMenu.add("Stop");

menuBar.add(formatMenu);

Tstart=new JButton("Start");
Tstop=new JButton("Stop");

toolBar = new JToolBar("Formatting");
toolBar.add("Start",Tstart);
Tstart.addActionListener(this);
formatMenu.addActionListener(this);
timer.addActionListener(new Main_Frame_Timer_ActionAdapter(this));

toolBar.addSeparator();


}

public void start() {

MainFrame example = new MainFrame();
example.pane = new JPanel();
example.pane.setPreferredSize(new Dimension(500, 500));
example.pane.setBorder(new BevelBorder(BevelBorder.LOWERED));
example.toolBar.setMaximumSize(example.toolBar.get Size());

JFrame frame = new JFrame("CA Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setJMenuBar(example.menuBar);
frame.getContentPane().add(example.toolBar, BorderLayout.NORTH);
frame.getContentPane().add(example.pane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public void stop() {

}
public void destroy() {

}
public void clear(){
Graphics g=pane.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, pane.getWidth(), pane.getHeight());
}
public void ShowCurrentRow(byte[] row,int time){
Graphics g=pane.getGraphics();
for(int i=0;i<row.length;++i){
if(row[i]>0)
g.fillRect(3*(10+i), 3*(time+5), 3, 3);
}
}



public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

if(e.getSource()==Tstart){

if(timer.isRunning()) timer.stop();
else{
if(timeStep>100){
ca.reset();
timeStep=0;
clear();
ShowCurrentRow(ca.getcurrentRow(),timeStep);
}
timer.start();
}

}

}

}



class Main_Frame_Timer_ActionAdapter implements ActionListener{
MainFrame adaptee;
public Main_Frame_Timer_ActionAdapter(MainFrame ad){
this.adaptee=ad;
}

public void actionPerformed(ActionEvent e) {
// TODO add timer event
adaptee.timerEvent(e);

}
}

//-----------Error----------
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at MainFrame.ShowCurrentRow(MainFrame.java:166)
at MainFrame.timerEvent(MainFrame.java:108)
at Main_Frame_Timer_ActionAdapter.actionPerformed(Mai nFrame.java:207)
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$DoPostEvent.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-30-2008, 10:12 PM
Senior Member
 
Join Date: Jul 2007
Posts: 930
hardwired is on a distinguished road
Code:
// <applet code="MFApplet" width="500" height="500"></applet> // run this at the prompt with: >appletviewer MFApplet.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.Timer; import javax.swing.border.BevelBorder; public class MFApplet extends JApplet implements ActionListener{ public JPanel pane; CA_1D2S3N ca=new CA_1D2S3N (50); int timeStep=101; public JMenuBar menuBar; public JToolBar toolBar; JButton Tstart; JButton Tstop; public static Timer timer=new Timer(100,null); public void timerEvent(ActionEvent e){ ca.TimeStep(); ShowCurrentRow(ca.getcurrentRow(),++timeStep); if (timeStep>100) timer.stop(); } // public MFApplet() { // this is a constructor for an app public void init() { // applet construction code goes here menuBar = new JMenuBar(); JMenu formatMenu = new JMenu("Timer"); formatMenu.add("Start"); formatMenu.add("Stop"); menuBar.add(formatMenu); Tstart=new JButton("Start"); Tstop=new JButton("Stop"); toolBar = new JToolBar("Formatting"); toolBar.add("Start",Tstart); Tstart.addActionListener(this); formatMenu.addActionListener(this); timer.addActionListener(new Main_Frame_Timer_ActionAdapter(this)); toolBar.addSeparator(); pane = new JPanel(); pane.setPreferredSize(new Dimension(500, 500)); pane.setBorder(new BevelBorder(BevelBorder.LOWERED)); Dimension size = toolBar.getSize(); Dimension prefSize = toolBar.getPreferredSize(); System.out.printf("toolBar size = [%2d, %2d]%n" + " prefSize = [%2d, %2d]%n", size.width, size.height, prefSize.width, prefSize.height); toolBar.setMaximumSize(toolBar.getSize()); setJMenuBar(menuBar); getContentPane().add(toolBar, BorderLayout.NORTH); getContentPane().add(pane, BorderLayout.CENTER); } public void start() { // This method is called everytime an applet is restored. // To run this applet as an application move the code that // was here off to a main method. } public void stop() {} public void destroy() {} public void clear(){ Graphics g=pane.getGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, pane.getWidth(), pane.getHeight()); } public void ShowCurrentRow(byte[] row,int time){ Graphics g=pane.getGraphics(); for(int i=0;i<row.length;++i){ if(row[i]>0) g.fillRect(3*(10+i), 3*(time+5), 3, 3); } } public void actionPerformed(ActionEvent e) { if(e.getSource()==Tstart){ if(timer.isRunning()) timer.stop(); else{ if(timeStep>100){ ca.reset(); timeStep=0; clear(); ShowCurrentRow(ca.getcurrentRow(),timeStep); } timer.start(); } } } /** Convenience main to run applet as an aplication. */ public static void main(String[] args) { MFApplet applet = new MFApplet(); Dimension d = applet.getPreferredSize(); System.out.printf("applet prefSize = [%d, %d]%n", d.width, d.height); applet.setPreferredSize(new Dimension(500, 500)); JFrame frame = new JFrame("CA Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(applet); frame.pack(); applet.init(); frame.setVisible(true); } } class Main_Frame_Timer_ActionAdapter implements ActionListener{ MFApplet adaptee; public Main_Frame_Timer_ActionAdapter(MFApplet ad){ this.adaptee=ad; } public void actionPerformed(ActionEvent e) { // TODO add timer event adaptee.timerEvent(e); } } class CA_1D2S3N { private byte[][]rows; private int curRow=0,nextRow=1; private int rowSize=50; private int cellStates=2; private byte[] rules={0,1,1,1,1,0,0,0}; public CA_1D2S3N(){} public CA_1D2S3N(int rSize){ rowSize=rSize; rows=new byte[2][]; rows[curRow] =new byte[rowSize]; rows[nextRow] =new byte[rowSize]; reset(); } public int getRowSize(){ return rowSize; } public void reset(){ for(int i=curRow;i<=nextRow;++i){ for(int j=0;j<rowSize;j++){ rows[i][j]=0; } } rows[curRow][(int)(rowSize/2)]=1; } public void TimeStep(){ for(int i=1;i<rowSize-2;++i){ int state=Map(rows[curRow],i); rows[1][i]=rules[state]; } for(int i=0;i<rowSize;i++){ rows[0][i]=rows[1][i]; } } int Map(byte []row,int i){ int x=4*row[i-1] + 2*row[i] + 1*row[i+1]; return x; } byte[] getcurrentRow(){ return rows[curRow].clone(); } }
Note what happens to your graphics after the applet is minimized/restored or partially un/covered. This happens when you draw inside event code. To avoid
this do your drawing inside the JPanel "pane"s paintComponent method.
In java pseudocode:
Code:
... public void init() { ... pane = new Pseudo(); ... class Pseudo extends JPanel { // Use booleans or other member variables to control // the state of this component. Set up the drawing // code inside paintComponent so it can/will // draw the current state of this compoinent at any time. /** override this method for custom drawing */ protected void paintComponent(Graphics g) { super.paintComponent(g); // do custom drawing here. } }
Then you alter/control/set the state/member variables in this class from your applet event code and tell it to repaint.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-01-2008, 11:14 AM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 223
DonCash will become famous soon enoughDonCash will become famous soon enough
Hello rohan,

Welcome to the Java Forums.

In future, please put the tags around your code! It stops errors and makes it much easier to read.
Attached Images
File Type: bmp codetags2.bmp (17.7 KB, 9 views)
__________________
Did this post help you? Please me! || Don't forget to: Mark your Thread as Solved
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Null pointer exception error brownie_jedi New To Java 3 03-15-2008 07:27 AM
Trouble with factory method - unhandled exception type Exception desmond5 New To Java 1 03-08-2008 07:41 PM
statement null pointer exception bbq Database 1 07-05-2007 05:23 AM
Execution cut Eric Advanced Java 1 06-27-2007 04:52 PM
Null pointer Exception peiceonly New To Java 2 04-06-2007 03:41 PM


All times are GMT +3. The time now is 10:52 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org