Total Newbie cant get his stuff right!!! problems with getParameter

Hi, I only started learning java about 3 hours ago... but I have good knowledge of PHP and Visual Basic, so getting started wasnt too hard.

I have one problem though - getParameter works when I am testing on my local machine, but when I upload to my site it doesnt work - It seems to raise an exception.

I am using NetBeans IDE, heres the code that doesnt work

package imagegallery;

import java.awt.*;

import java.applet.*;

import java.net.*;

import java.awt.event.*;

/**

*

* @author matt@dastudios.org

*/

publicclass clsImageGalleryextends Applet{

publicvoid paint(Graphics g){

String s1 = getParameter("top");

g.drawString(s1, 20, 20);

}

}

and the php I am using:

<HTML>

<HEAD>

<TITLE>Applet HTML Page</TITLE>

</HEAD>

<BODY>

<!--

*** GENERATED applet HTML launcher - DO NOT EDIT IN'BUILD' FOLDER ***

If you need to modifythis HTML launcher file (e.g., to add applet parameters),

copy it to where your appletclass is found in the SRC folder. If youdo this,

the IDE will use it when you run or debug the applet.

Tip: To exclude an HTML launcher from the JAR file, use exclusion filters in

the Packaging page in the Project Properties dialog.

For more information see the online help.

-->

<H3><HR WIDTH="100%">Applet HTML Page<HR WIDTH="100%"></H3>

<P>

<APPLET codebase="classes" code="imagegallery/clsImageGallery.class" width=350 height=200>

<?php

global $top;

$top=0;

for($i=1;$i<=999;$i++){

if($i<10){

$cName="00".$i;

}else{

if($i<100){

$cName="0".$i;

}else{

$cName=$i;

}

}

if(file_exists("./images/".$cName.".jpg")){

$top++;

}else{

break;

}

}

echo'<PARAM NAME=top value="'.$top.'" />';

?>

</APPLET>

</P>

<HR WIDTH="100%"><FONT SIZE=-1><I>Generated by NetBeans IDE</I></FONT>

</BODY>

</HTML>

If anyone can help I would be very happy. It seems pretty simple though.

[3853 byte] By [matt_dastudiosa] at [2007-10-2 15:07:03]
# 1
Did you check the generated page source to see if it had the appropriate parameter tag? This isn't a PHP forum - can't help you if that's where the problem lies.Otherwise, please post the full stack trace (exception) message.
dcmintera at 2007-7-13 13:59:04 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks for your help, while I was looking for the trace I found the problem - just a silly mistake I made while uploading (wrong directory)
matt_dastudiosa at 2007-7-13 13:59:04 > top of Java-index,Java Essentials,Java Programming...
# 3

ok, im using this code now:

package imagegallery;

import java.awt.*;

import java.applet.*;

import java.net.*;

import java.awt.event.*;

/**

*

* @author matt@dastudios.org

*/

public class clsImageGallery extends Applet implements ActionListener {

Button prevButton;

Button nextButton;

URL base;

Image imgDisp[];

MediaTracker mt;

int curImage;

int iTop;

public void init() {

setLayout(new FlowLayout());

prevButton=new Button("<--");

nextButton=new Button("-->");

add(prevButton);

add(nextButton);

prevButton.addActionListener(this);

nextButton.addActionListener(this);

try{base=getDocumentBase();}catch(Exception e){}

mt = new MediaTracker(this);

iTop=Integer.valueOf(getParameter("top"));

String sTmp;

String sPath;

if(iTop >= 1){

for(int i=1;i<=iTop;i++){

if(i<10){

sTmp="00"+String.valueOf(i);

}else{

if(i<100){

sTmp="0"+String.valueOf(i);

}else{

sTmp=String.valueOf(i);

}

}

sPath="images/"+sTmp+".jpg";

imgDisp[i]=getImage(base,sPath);

mt.addImage(imgDisp[i],i);

}

if(iTop > 3){

try{mt.waitForID(3,10000);}catch(InterruptedException e){}

}else{

try{mt.waitForAll(10000);}catch(InterruptedException e){}

}

curImage=1;

prevButton.setEnabled(false);

if(iTop==1){

nextButton.setEnabled(false);

}

}

}

public void paint(Graphics g){

g.drawImage(imgDisp[curImage], 20, 20, this);

g.drawString(getParameter("top"), 20, 20);

}

public void actionPerformed(ActionEvent evt){

if(evt.getSource()==nextButton){

//next photo

if((curImage+1)>iTop){

nextButton.setEnabled(false);

}else{

nextButton.setEnabled(true);

}

curImage++;

repaint();

}else{

if(evt.getSource()==prevButton){

//previous photo

if((curImage-1)<1){

prevButton.setEnabled(false);

}else{

prevButton.setEnabled(true);

}

curImage--;

repaint();

}

}

}

}

same php file, this is the trace:

java.lang.NullPointerException

at imagegallery.clsImageGallery.init(clsImageGallery.java:45)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Exception in thread "thread applet-imagegallery/clsImageGallery.class" java.lang.NullPointerException

at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)

at sun.plugin.AppletViewer.showAppletException(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

matt_dastudiosa at 2007-7-13 13:59:04 > top of Java-index,Java Essentials,Java Programming...
# 4

Don't know (it depends what's on line 45, where the exception occurs).

However, this is extremely poor coding practice:

try{base=getDocumentBase();}catch(Exception e){}

You should report the error if it occurs, not hide it. Any subsequent code that depends upon the base variable should be inside the try block, since it can't proceed without it.

dcmintera at 2007-7-13 13:59:04 > top of Java-index,Java Essentials,Java Programming...
# 5

ok, there is nothing wrong with the try{} statement, however I have fixed it as you suggested. Here is the code surrounding where the error is, line 45 has a comment after the statement:

for(int i=1;i<=iTop;i++){

if(i<10){

sTmp="00"+String.valueOf(i);

}else{

if(i<100){

sTmp="0"+String.valueOf(i);

}else{

sTmp=String.valueOf(i);

}

}

sPath="images/"+sTmp+".jpg";

imgDisp[i]=getImage(base,sPath);

mt.addImage(imgDisp[i],i);

}

Thanks for your continued help

matt_dastudiosa at 2007-7-13 13:59:04 > top of Java-index,Java Essentials,Java Programming...
# 6
oops! this is the line:imgDisp[i]=getImage(base,sPath);Image objects can be referenced as arrays, can't they!? Or is the syntax different? When it compiles there is no errors, but upon running I get the mentioned exception....
matt_dastudiosa at 2007-7-13 13:59:04 > top of Java-index,Java Essentials,Java Programming...
# 7
you need to initialise your array.you are trying to set imgDisp without ever creating a new array.for example -imgDisp = new Image[10];
phawdona at 2007-7-13 13:59:04 > top of Java-index,Java Essentials,Java Programming...
# 8
so did that work?....
phawdona at 2007-7-13 13:59:04 > top of Java-index,Java Essentials,Java Programming...