473,385 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

difficult design problem?

// I want to place a panel which contains two canvases (a red one and
a blue one) on screen,
// along with two buttons. When a button is pressed the appropriate
message should be
// printed in the canvas. The code compiles okay, but I get a
run-time NullPointerException.
// I don't understand why - could anyone please explain the problem
and suggest a solution?
// Thanks very much for any help,
// Gerard
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

class TypeOneApplet extends Applet implements ActionListener{

public final int CLEAR = 1;
public final int DULL = 2;
Button b1, b2;
Panel pan;
P p;
int theAction = 1;

public void actionPerformed( ActionEvent e){
if( e.getSource() == b1)
theAction = CLEAR;
else
theAction = DULL;
}

void methodOne(){ System.out.println(" method One"); }
void methodTwo(){ System.out.println(" method Two"); }
}

public class TypeTwoApplet extends TypeOneApplet {

public void init(){
b1 = new Button("clear");
b2 = new Button("dull");
b1.addActionListener(this);
b2.addActionListener(this);
this.add( b1);
this.add( b2);
pan = new Panel();
p = new P( this);
pan.add( p);
this.add( pan);
System.out.println("exit init");
}

void methodTwo(){ System.out.println(" method Two B"); }
}

class M extends Canvas implements MouseListener{
TypeOneApplet toa;
String message;

public M(){}
public M( TypeOneApplet t){
toa = t;
message = "ready";
}

public void mousePressed( MouseEvent evt){
if( toa.theAction == toa.CLEAR)
message = "clear";
else
message = "dull";
repaint();
}
public void mouseClicked( MouseEvent e) {}
public void mouseEntered( MouseEvent e) {}
public void mouseExited( MouseEvent e) {}
public void mouseReleased( MouseEvent e) {}

public void paint( Graphics g){
setBackground( Color.red);
setForeground( Color.black);
g.drawString( message, 5, 5);
}
}

class M2 extends M{
TypeTwoApplet atoa;

public M2( TypeTwoApplet t){
atoa = t;
}

public void mousePressed( MouseEvent evt){
if( atoa.theAction == toa.CLEAR)
message = "clear 2";
else
message = "dull 2";
repaint();
}

public void paint( Graphics g){
setBackground( Color.blue);
setForeground( Color.black);
g.drawString( message, 5, 5);
}
}

class P extends Panel{
M [] m;

public P( TypeTwoApplet t){
m[0] = new M(t);
m[1] = new M2(t);
add( m[0]);
add( m[1]);
}
}
Jul 17 '05 #1
4 1641
<ge******@indigo.ie> wrote in message
news:vy*******************@news.indigo.ie...
The code compiles okay, but I get a
run-time NullPointerException.


Please give the 1st lines of the exception that apply to your code and
indicate which line of the code the line numbers in the exception trace
refer to.

--
Mike W
Jul 17 '05 #2
Thanks for the suggestion about including error message. Here's the
run-time NullPointerException, which reads:
at P.(init)(TypeTwoApplet.java:107)
at TypeTwoApplet.init(TypeTwoApplet.java:43)

Regarding the previous subject line, I've stripped down and extracted
the idea of a much larger program to show the overall design, as I
wasn't certain that I was using the classes correctly. For
instance, as init() seems to be causing a problem, I wondered if it is
necessary to have a default init() in an applet in the same way that I
must supply a default constructor for class M. And I wondered if
class M2 will communicate with the applet subclasses (as I hope it
will).

As for posting to the three newsgroups, I'm unsure if it is supposed
to be better etiquette to post to one, read the replies, then post to
another, read the replies there, etc? I've found in the past that by
posting to two or three closely related groups at once I get
different, useful replies in each group.

Thanks again for any help,
Gerard
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

class TypeOneApplet extends Applet implements ActionListener{

public final int CLEAR = 1;
public final int DULL = 2;
Button b1, b2;
Panel pan;
P p;
int theAction = 1;

public void actionPerformed( ActionEvent e){
if( e.getSource() == b1)
theAction = CLEAR;
else
theAction = DULL;
}

void methodOne(){ System.out.println(" method One"); }
void methodTwo(){ System.out.println(" method Two"); }
}

public class TypeTwoApplet extends TypeOneApplet {

public void init(){
b1 = new Button("clear");
b2 = new Button("dull");
b1.addActionListener(this);
b2.addActionListener(this);
this.add( b1);
this.add( b2);
pan = new Panel();
p = new P( this);
pan.add( p);
this.add( pan);
System.out.println("exit init");
}

void methodTwo(){ System.out.println(" method Two B"); }
}

class M extends Canvas implements MouseListener{
TypeOneApplet toa;
String message;

public M(){}
public M( TypeOneApplet t){
toa = t;
message = "ready";
}

public void mousePressed( MouseEvent evt){
if( toa.theAction == toa.CLEAR)
message = "clear";
else
message = "dull";
repaint();
}
public void mouseClicked( MouseEvent e) {}
public void mouseEntered( MouseEvent e) {}
public void mouseExited( MouseEvent e) {}
public void mouseReleased( MouseEvent e) {}

public void paint( Graphics g){
setBackground( Color.red);
setForeground( Color.black);
g.drawString( message, 5, 5);
}
}

class M2 extends M{
TypeTwoApplet atoa;

public M2( TypeTwoApplet t){
atoa = t;
}

public void mousePressed( MouseEvent evt){
if( atoa.theAction == toa.CLEAR)
message = "clear 2";
else
message = "dull 2";
repaint();
}

public void paint( Graphics g){
setBackground( Color.blue);
setForeground( Color.black);
g.drawString( message, 5, 5);
}
}

class P extends Panel{
M [] m;

public P( TypeTwoApplet t){
m[0] = new M(t);
m[1] = new M2(t);
add( m[0]);
add( m[1]);
}
}

Jul 17 '05 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ge******@indigo.ie wrote:

[snip]
class P extends Panel{
M [] m;

public P( TypeTwoApplet t){
m[0] = new M(t);
m[1] = new M2(t);
add( m[0]);
add( m[1]);
}
}


Hi,
I see the problem. You declared a reference to an array of M, but you
never created the array before trying to store objects in it. You
should change the declaration to also create the actual array:

M[] m = new M[2];

I think this should be the only problem.

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/M2rFwxczzJRavJYRAjzAAJ9JXdDsXo0kHpkqnQo6iRnlbWT7Aw CaA9mp
+gBJroLZcoY1zsJtovtW/PY=
=2kgz
-----END PGP SIGNATURE-----
Jul 17 '05 #4
On Fri, 08 Aug 2003 13:26:35 GMT, ge******@indigo.ie wrote or quoted :
. The code compiles okay, but I get a
run-time NullPointerException.


When it throws the null pointer exception you will get a stack trace
that tells you exactly where it occurred and how you got there. That
it what you have to look at.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Spammay Blockay | last post by:
I've been tasked with doing technical interviews at my company, and I have generally ask a range of OO, Java, and "good programming technique" concepts. However, one of my favorite exercises I...
9
by: Robert Brown | last post by:
Our customer (of our ecommerce system) wants to be able to preserve deleted entities in the database so that they can do reporting, auditing etc. The system is quite complex where each end user...
3
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
7
by: PerryC | last post by:
I have search googles and there are hundreds of tips about AllowByPassKey... however, none works for me... well, perhaps I am too new to such high level functionality that it just does not make...
5
by: Bob | last post by:
Hi Everybody Difficult question Has anyone else used the "Using the Tab control as a container" database example that comes with the above book chapter 7 on the accompanying disc. It is a...
4
by: d.p. | last post by:
Hi all, I'm using MS Access 2003. Bare with me on this description....here's the situation: Imagine insurance, and working out premiums for different insured properties. The rates for calculating...
4
by: n | last post by:
Hello! Here is a problem I hope you can point me to a solution. It Problem: A teacher needs to know which lesson to teach. A school has a curriculum with 26 lessons, A-Z. For a given class,...
21
by: Tarscher | last post by:
Hi all, I have events containing attendees (events has many attendees). The attendee table tells whether a user will attend the event or not. I want to build a query that returns all the...
9
by: AceKnocks | last post by:
I am working on a framework design problem in which I have to design a C++ based framework capable of solving three puzzles for now but actually it should work with a general puzzle of any kind and I...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.