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.
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)
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