Please really need help :NullPointerException with my applet

  • Thread starter Emilie Dorchies via JavaKB.com
  • Start date
E

Emilie Dorchies via JavaKB.com

The following code is ought to init my applet displaying a solution to the CIgarette Smokers Concurrency Problem.
But it returns a NullPointerException while trying to load the images. I've tried also with getDocumentBase : same error.
It drives me insane. Anyone 's got a workaround? Every contribution will be helpful.
Thank you in advance,

Emilie

public class CigarApplet
extends Applet {

public static final int NUM_SMOKER = 3;
public static final int NUM_INGREDIENT = 1;

public Lock lock_ = new Lock();
public Lock matches_ = new Lock();
public Lock paper_ = new Lock();
public Lock tobacco_ = new Lock();

Image[] imgs = new Image[5];
URL[] image;

CigarCanvas display;
Thread[] smoke = new Thread[NUM_SMOKER];
Thread agent = new Thread();
Scrollbar slider;
Button restart;
Button freeze;

boolean isStandalone;

//Initialize the applet
public void init() {

try {

MediaTracker mt = new MediaTracker(this);
image[0] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/agent.gif");
imgs[0] = getImage(image[0]);
mt.addImage(imgs[0], 0);

image[1] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/matches_waiting.gif");
imgs[1] = getImage(image[1]);
mt.addImage(imgs[1], 1);

image[2] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/paper_waiting.gif");
imgs[2] = getImage(image[2]);
mt.addImage(imgs[2], 2);

image[3] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/tobacco_waiting.gif");
imgs[3] = getImage(image[3]);
mt.addImage(imgs[3], 3);

image[4] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/matches_smoking.gif");
imgs[4] = getImage(image[4]);
mt.addImage(imgs[4], 4);

image[5] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/paper_smoking.gif");
imgs[5] = getImage(image[5]);
mt.addImage(imgs[5], 5);

image[6] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/tobacco_smoking.gif");
imgs[6] = getImage(image[6]);
mt.addImage(imgs[6], 6);

image[7] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/matches.gif");
imgs[7] = getImage(image[7]);
mt.addImage(imgs[7], 7);

image[8] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/paper.gif");
imgs[8] = getImage(image[8]);
mt.addImage(imgs[8], 8);

image[9] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/tobacco.gif");
imgs[9] = getImage(image[9]);
mt.addImage(imgs[9], 9);

try {
mt.waitForAll();
}
catch (java.lang.NullPointerException e) {
e.printStackTrace();
}
display.initPlacing();

JPanel p1 = new JPanel();
setLayout(new BorderLayout());
p1.add(slider = new Scrollbar(Scrollbar.HORIZONTAL, 50, 5, 0, 100));
p1.add(restart = new Button("Restart"));
p1.add(freeze = new Button("Freeze"));
p1.setLayout(new BorderLayout());
display = new CigarCanvas(this);
display.setSize(300, 320);
p1.add("Center", display);

}
catch (Exception e) {
e.printStackTrace();
}
}
 
E

Eric Sosman

Emilie said:
The following code is ought to init my applet displaying a solution to the CIgarette Smokers Concurrency Problem.
But it returns a NullPointerException while trying to load the images. I've tried also with getDocumentBase : same error.
It drives me insane. Anyone 's got a workaround? Every contribution will be helpful.
Thank you in advance,

Emilie

public class CigarApplet
extends Applet {

public static final int NUM_SMOKER = 3;
public static final int NUM_INGREDIENT = 1;

public Lock lock_ = new Lock();
public Lock matches_ = new Lock();
public Lock paper_ = new Lock();
public Lock tobacco_ = new Lock();

Image[] imgs = new Image[5];
URL[] image;

This creates a reference called `image' that is able
to point to an array of URL objects. No such array has
been created yet, and `image' has the value `null'.
CigarCanvas display;
Thread[] smoke = new Thread[NUM_SMOKER];
Thread agent = new Thread();
Scrollbar slider;
Button restart;
Button freeze;

boolean isStandalone;

//Initialize the applet
public void init() {

try {

MediaTracker mt = new MediaTracker(this);
image[0] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/agent.gif");

Since `image' is still `null', it does not point to an
array of any kind. When you try to access the zeroth element
of that non-existent array, you get a NullPointerException.
imgs[0] = getImage(image[0]);
mt.addImage(imgs[0], 0);
[...]
imgs[5] = getImage(image[5]);

Once you solve the NullPointerException, your next problem
will be the ArrayIndexOutOfBoundsException you get when you try
to access the sixth element of this five-element array ...

By the way, have you ever heard of something called a "loop?"
The code you're using for all ten of these images differs only
in the URL strings and in the array indices. Why not just make
an array of the strings and then write a loop that repeats the
same code ten times, once for each string? It's not only boring
to write the exact same code ten times in a row, but you'll find
that it's also error-prone.
 
A

Andrew Thompson

The following code is ought to init my applet displaying a
solution to the CIgarette Smokers Concurrency Problem.

Please put 'newlines' before 72 characters Emilie. Usually I
recommend that folks set their news-client to do it for them,
but I notice you are using a web-based interface to Usenet, so
you will need to do it manually.
But it returns ...

More commonly referred to as 'throws'
..a NullPointerException while trying to load the images.
..I've tried also with getDocumentBase : same error.
It drives me insane. Anyone 's got a workaround?

Sensible code should do the trick, no 'workarounds' required.

But before we get to a solution there are some (OK many)
things that need to be cleared up.

- You are posting to a group for advanced questions about
Java, whereas it is apparent that you are just learning,
so a better group to post to for the moment is
<http://www.physci.org/codes/javafaq.jsp#cljh>
On your web-based interface to usenet, it is referred to as
'First Aid'.

- Don't mix swing and the AWT, they do not play well together.

- Please post an SSCCE[1], rather than 102 lines of code that do
not compile, due to..
- lack of import statements and a closing '}'
- Missing two classes, 'Lock' and 'CigarCanvas'

[1] <http://www.physci.org/codes/sscce.jsp>

- When discussing applet problems in particular, always
mention what OS and version, browser and version, Java
maker and version. The last can be answered here.
<http://www.physci.org/pc/property.jsp?prop=java.version+java.vendor>

- As mentioned in the SSCCE document, when dealing with applets,
upload the applet (broken or whatever) and source (and images)
to a free web-server like GeoCities so that people who are
helping you can see it break for themselves. Link to the code
from the applet page.

- Read the FAQ referenced above, from start to finish.
It mentions both applets (many times) and how to find resources.

See you over on 'First Aid' with those things fixed up and
we should be able to fix the problem pronto.

HTH
 
E

Emilie Dorchies via JavaKB.com

Thank you for all these advices.
See u on On the First Aid
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top