Home arrow static arrow Java Programming [Archive] - Nullpointerexc. on applet - Please Help !
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Nullpointerexc. on applet - Please Help !
5 Duke Stars available
This topic has 4 replies on 1 page.

Posts:61
Registered: 8/20/01
Nullpointerexc. on applet - Please Help !   
Jul 30, 2004 2:09 AM



 
Hi,
I've done a program, which is like a server - It opens a port and if somebody connects on it, it shows a website - well - it does.
The problem: on the website is a java-applet and I get a nullpointerexception if I want to see it.
If I only watch the website alone - it works?!

Here is the code from the server and the website, etc.
Thx in advance.

[code]

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class Haupt extends Frame implements ActionListener
{
MenuItem menuFileExit = null;
MenuItem menuFileBlah = null;
TextField Alpha = new TextField("Server ist initialisiert");
TextField Beta = new TextField("Kein Applettkontakt");
TextField leer = new TextField();
BufferedInputStream readFile = new BufferedInputStream(null);
private Socket client = null;
private ObjectInputStream ois = null;
private ObjectOutputStream oos = null;
private ServerSocket dateServer;
Versuchen a= new Versuchen();
//Applettlistener b= new Applettlistener();


/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Server f�r Port 3000 - Internet Explorer
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/
public class Versuchen extends Thread
{
public void Versuche()
{
try
{
//opens up a Port - 3000
dateServer = new ServerSocket(3000);
Alpha.setText(Alpha.getText() +"\n" + ("Server listening on port 3000."));
a.start();
}
catch(Exception e)
{
Alpha.setText(e.toString());
}
}

public void run()
{
Alpha.setText(Alpha.getText() + "\n" + "Waiting for connections.");
while(true)
{
try
{

Socket client = dateServer.accept();
Alpha.setText(Alpha.getText() + "\n" + ("Accepted a connection from: "+
client.getInetAddress()+client.getLocalPort()));
Schliessen(client);
} catch(Exception e) {}

}
}

public void Schliessen(Socket clientSocket) {

client = clientSocket;
try {
oos = new ObjectOutputStream(client.getOutputStream());
File Html = new File("Html.htm");
try
{
BufferedReader in = new BufferedReader(new FileReader(Html));
String a = new String(in.readLine());
in.close();
oos.writeUTF(a);
}catch(Exception ioe)
{ {
} }


Alpha.setText(Alpha.getText() + "\n" + "Htmlsheet sendet to IE");
oos.flush();

// close streams and connections
oos.close();
client.close();

} catch(Exception e)
{
Alpha.setText(e.toString());
}
}

}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="Exit")
{
try{
client.close();
}
catch(Exception de){}
System.exit(0);
}
if(e.getActionCommand()=="Status abrufen")
{
Alpha.setText("Status kann noch nicht abgerufen werden");
}
if(e.getActionCommand()=="Listen to Port")
{
Alpha.setText("Server wurde gestartet");

a.Versuche();
}
}

/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Server f�r Port 3001 - Applet
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/

/*
public class Applettlistener extends Thread
{
public void Versuche1()
{
try
{
//opens up a Port - 3001
dateServer = new ServerSocket(3001);
Beta.setText(Alpha.getText() +"\n" + ("Server listening for applet on 3001."));

}
catch(Exception e)
{
Beta.setText(e.toString());
}
}

public void run()
{
Beta.setText(Beta.getText() + "\n" + "Waiting for connections.");
while(true)
{
try
{

Socket client = dateServer.accept();
Beta.setText(Beta.getText() + "\n" + ("Accepted a connection from applet: "+
client.getInetAddress()+client.getLocalPort()));
Schliessen(client);
} catch(Exception e) {}

}
}

public void Schliessen(Socket clientSocket) {

client = clientSocket;
try {
oos = new ObjectOutputStream(client.getOutputStream());
oos.writeBytes("Ich bin ein Server");
Beta.setText(Beta.getText() + "\n" + "Htmlsheet sendet to IE");
oos.flush();

// close streams and connections
oos.close();
client.close();
} catch(Exception e)
{
Beta.setText(e.toString());
}
}

}

*/

public Haupt()
{

super("ServerApplication");
super.setBackground(new Color(50,50,50));
MenuBar menuBar = new MenuBar( );
Menu menuFile = new Menu( );
MenuItem menuFileExit = new MenuItem( );
MenuItem menuFileBlah = new MenuItem( );
menuFile.setLabel("File");
menuFileBlah.setLabel("Status abrufen");
menuFileExit.setLabel("Exit");
Menu menuFile2 = new Menu( );
menuFile2.setLabel("Edit");
menuFile.add(menuFileBlah);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
menuBar.add(menuFile2);
setMenuBar(menuBar);

menuFileExit.addActionListener(this);
menuFileBlah.addActionListener(this);
Button Reading = new Button("Listen to Port");
Reading.setSize(100,30);
Reading.setLocation(200,460);
Reading.addActionListener(this);

TextField t1 = new TextField("ServerCommunication");
t1.setSize(150,20);
t1.setLocation(10,55);
t1.setEditable(false);
t1.setForeground(new Color(255,0,0));

TextField t2 = new TextField("Applet to Server");
t2.setSize(150,20);
t2.setLocation(500,55);
t2.setEditable(false);
t2.setForeground(new Color(255,0,0));

Alpha.setSize(380,355);
Alpha.setBackground(new Color(130,130,130));
Alpha.setLocation(10,85);

Beta.setSize(380,355);
Beta.setBackground(new Color(130,130,130));
Beta.setLocation(500,85);

super.add(Alpha);
super.add(Beta);
super.add(Reading);
super.add(t1);
super.add(t2);
super.add(leer);

}

public static void main(String[] args)
{
Haupt Fenster=new Haupt();
Fenster.setSize(1000,500);
Fenster.show();

Fenster.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{

System.exit(0);
}
});
}

}

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class Haupt extends Frame implements ActionListener
{
MenuItem menuFileExit = null;
MenuItem menuFileBlah = null;
TextField Alpha = new TextField("Server ist initialisiert");
TextField Beta = new TextField("Kein Applettkontakt");
TextField leer = new TextField();
BufferedInputStream readFile = new BufferedInputStream(null);
private Socket client = null;
private ObjectInputStream ois = null;
private ObjectOutputStream oos = null;
private ServerSocket dateServer;
Versuchen a= new Versuchen();
//Applettlistener b= new Applettlistener();


/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Server f�r Port 3000 - Internet Explorer
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/
public class Versuchen extends Thread
{
public void Versuche()
{
try
{
//opens up a Port - 3000
dateServer = new ServerSocket(3000);
Alpha.setText(Alpha.getText() +"\n" + ("Server listening on port 3000."));
a.start();
}
catch(Exception e)
{
Alpha.setText(e.toString());
}
}

public void run()
{
Alpha.setText(Alpha.getText() + "\n" + "Waiting for connections.");
while(true)
{
try
{

Socket client = dateServer.accept();
Alpha.setText(Alpha.getText() + "\n" + ("Accepted a connection from: "+
client.getInetAddress()+client.getLocalPort()));
Schliessen(client);
} catch(Exception e) {}

}
}

public void Schliessen(Socket clientSocket) {

client = clientSocket;
try {
oos = new ObjectOutputStream(client.getOutputStream());
File Html = new File("Html.htm");
try
{
BufferedReader in = new BufferedReader(new FileReader(Html));
String a = new String(in.readLine());
in.close();
oos.writeUTF(a);
}catch(Exception ioe)
{ {
} }


Alpha.setText(Alpha.getText() + "\n" + "Htmlsheet sendet to IE");
oos.flush();

// close streams and connections
oos.close();
client.close();

} catch(Exception e)
{
Alpha.setText(e.toString());
}
}

}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="Exit")
{
try{
client.close();
}
catch(Exception de){}
System.exit(0);
}
if(e.getActionCommand()=="Status abrufen")
{
Alpha.setText("Status kann noch nicht abgerufen werden");
}
if(e.getActionCommand()=="Listen to Port")
{
Alpha.setText("Server wurde gestartet");

a.Versuche();
}
}

/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Server f�r Port 3001 - Applet
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/

/*
public class Applettlistener extends Thread
{
public void Versuche1()
{
try
{
//opens up a Port - 3001
dateServer = new ServerSocket(3001);
Beta.setText(Alpha.getText() +"\n" + ("Server listening for applet on 3001."));

}
catch(Exception e)
{
Beta.setText(e.toString());
}
}

public void run()
{
Beta.setText(Beta.getText() + "\n" + "Waiting for connections.");
while(true)
{
try
{

Socket client = dateServer.accept();
Beta.setText(Beta.getText() + "\n" + ("Accepted a connection from applet: "+
client.getInetAddress()+client.getLocalPort()));
Schliessen(client);
} catch(Exception e) {}

}
}

public void Schliessen(Socket clientSocket) {

client = clientSocket;
try {
oos = new ObjectOutputStream(client.getOutputStream());
oos.writeBytes("Ich bin ein Server");
Beta.setText(Beta.getText() + "\n" + "Htmlsheet sendet to IE");
oos.flush();

// close streams and connections
oos.close();
client.close();
} catch(Exception e)
{
Beta.setText(e.toString());
}
}

}

*/

public Haupt()
{

super("ServerApplication");
super.setBackground(new Color(50,50,50));
MenuBar menuBar = new MenuBar( );
Menu menuFile = new Menu( );
MenuItem menuFileExit = new MenuItem( );
MenuItem menuFileBlah = new MenuItem( );
menuFile.setLabel("File");
menuFileBlah.setLabel("Status abrufen");
menuFileExit.setLabel("Exit");
Menu menuFile2 = new Menu( );
menuFile2.setLabel("Edit");
menuFile.add(menuFileBlah);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
menuBar.add(menuFile2);
setMenuBar(menuBar);

menuFileExit.addActionListener(this);
menuFileBlah.addActionListener(this);
Button Reading = new Button("Listen to Port");
Reading.setSize(100,30);
Reading.setLocation(200,460);
Reading.addActionListener(this);

TextField t1 = new TextField("ServerCommunication");
t1.setSize(150,20);
t1.setLocation(10,55);
t1.setEditable(false);
t1.setForeground(new Color(255,0,0));

TextField t2 = new TextField("Applet to Server");
t2.setSize(150,20);
t2.setLocation(500,55);
t2.setEditable(false);
t2.setForeground(new Color(255,0,0));

Alpha.setSize(380,355);
Alpha.setBackground(new Color(130,130,130));
Alpha.setLocation(10,85);

Beta.setSize(380,355);
Beta.setBackground(new Color(130,130,130));
Beta.setLocation(500,85);

super.add(Alpha);
super.add(Beta);
super.add(Reading);
super.add(t1);
super.add(t2);
super.add(leer);

}

public static void main(String[] args)
{
Haupt Fenster=new Haupt();
Fenster.setSize(1000,500);
Fenster.show();

Fenster.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{

System.exit(0);
}
});
}

}

<HTML><BODY bgcolor=black text=yellow><applet code=Internet.class width=250 height=250></applet></BODY></HTML>

 

Posts:18,384
Registered: 21.03.00
Re: Nullpointerexc. on applet - Please Help !   
Jul 30, 2004 4:07 AM (reply 1 of 4)



 
Hi,

Show us the stacktrace.

/Kaj
 

Posts:61
Registered: 8/20/01
Re: Nullpointerexc. on applet - Please Help !   
Jul 30, 2004 7:15 AM (reply 2 of 4)



 
Well dont know what the stacktrace is excaclty.. but I think this...

java.lang.ClassNotFoundException: Internet.class

at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:153)

at sun.plugin.security.PluginClassLoader.findClass(PluginClassLoader.java:168)

at java.lang.ClassLoader.loadClass(ClassLoader.java:299)

at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:114)

at java.lang.ClassLoader.loadClass(ClassLoader.java:255)

at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:506)

at sun.applet.AppletPanel.createApplet(AppletPanel.java:566)

at sun.plugin.AppletViewer.createApplet(AppletViewer.java:1775)

at sun.applet.AppletPanel.runLoader(AppletPanel.java:495)

at sun.applet.AppletPanel.run(AppletPanel.java:292)

at java.lang.Thread.run(Thread.java:536)

Caused by: java.net.SocketException: Software caused connection abort: recv failed

at java.net.SocketInputStream.socketRead0(Native Method)

at java.net.SocketInputStream.read(SocketInputStream.java:129)

at java.io.BufferedInputStream.fill(BufferedInputStream.java:183)

at java.io.BufferedInputStream.read1(BufferedInputStream.java:222)

at java.io.BufferedInputStream.read(BufferedInputStream.java:277)

at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:741)

at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:702)

at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:712)

at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:583)

at sun.plugin.net.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:384)

at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:238)

at sun.applet.AppletClassLoader.getBytes(AppletClassLoader.java:250)

at sun.applet.AppletClassLoader.access$100(AppletClassLoader.java:42)

at sun.applet.AppletClassLoader$1.run(AppletClassLoader.java:143)

at java.security.AccessController.doPrivileged(Native Method)

at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:140)

... 10 more

well this time (he even doesn't find the class - strange ;)

and I think I forgot the code for the applet.

[code]
import java.applet.*;

import java.awt.*;


public class Internet extends Applet

{

public void init()

{
super.setBackground(new Color(155,1,1));
// Place initialization cycle code here.

}


public void start()

{

// Place start cycle code here.

}


public void paint(Graphics g)

{

// Place paint cycle code here.

}


public void stop()

{

// Place stop cycle code here.

}

public void destroy()

{

// Place destroy cycle code here.

}

}
[code]

 

Posts:105
Registered: 6/8/04
Re: Nullpointerexc. on applet - Please Help !   
Jul 30, 2004 7:39 AM (reply 3 of 4)



 
That error is probably not related to your Java code, but rather your HTML and the files on your server.

Is the Internet.class file in the same folder as the HTML file in which it is embedded?
 

Posts:61
Registered: 8/20/01
Re: Nullpointerexc. on applet - Please Help !   
Jul 31, 2004 3:52 AM (reply 4 of 4)



 
All files are in the same folder - is it possible that IE shows the correct think but there are hidden symbols anywhere in the code, which cannot be shown in Editor?!
Thx
 
This topic has 4 replies on 1 page.