![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
URL class
Previously, I made a post because I had an error in one of my programs. I wasn't too sure what the problem was so I made many different posts in my thread. Now I brought this down to one problem .
For some reason the code below works. However, making an object out of this class doesn't. This works alone without any errors: import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class ReadFile extends javax.swing.JApplet //Applet
{
StringBuffer buf;
String FileToRead = "testFile.txt";
String fileData [] = new String [1];
int countLines = 2;
//--------------------------------------------------------
public void init ()
{
//public ReadFile (URL codebase)
//{
String line;
URL url = null;
try
{
//System.out.println ("" + getCodeBase ());
url = new URL (getCodeBase (), FileToRead);
//url = new URL (codebase, FileToRead);
}
catch (MalformedURLException e)
{
System.out.println ("Malformed URL ");
stop ();
}
try
{
InputStream in = url.openStream ();
BufferedReader dis = new BufferedReader (new InputStreamReader (in)); // check number of lines
buf = new StringBuffer ();
countLines = 0;
while ((line = dis.readLine ()) != null)
{
countLines = countLines + 1;
}
in.close ();
dis.close ();
fileData = new String [countLines];
in = url.openStream ();
dis = new BufferedReader (new InputStreamReader (in)); // now read the file
for (int v = 0 ; v < countLines ; v++)
{
fileData [v] = dis.readLine ();
}
dis.close ();
}
catch (IOException e)
{
}
// Load the file into the TextArea.
//ta.append(buf.toString ());
repaint ();
}
public void paint (Graphics screen)
{
{
Graphics2D screen2D = (Graphics2D) screen;
String tempStr = buf.toString ();
//screen2D.drawString (tempStr, 10, 10);
for (int q = 0 ; q < countLines ; q++)
{
screen2D.drawString (fileData [q], 10, 10 + q * 15);
}
}
}
int getLines ()
{
return countLines;
}
String getData ()
{
return fileData [1];
}
int getInt ()
{
return countLines;
}
}Now that works, please feel free to download the .zip containning the html file + the txt file so you can compile this. The above works without any errors what so ever. So why doesn't the below code work? I seem to get an error at the "getCodeBase()" command whenever the class above becomes a helper class to another class such as "MainClass.java". Here's what I mean, below MainSomething.java is creating a FileRead Object. However, now the "getCodeBase()" is popping up as an error. Which wasn't an issue until I made it a helper class. //MainSomething.java
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
class MainSomething extends Applet
{
ReadFile testing;
int tempInt = 1;
StringBuffer buf;
int something = 1;
String fileDat = new String ();
public void init ()
{
//ReadFileTemp testing = new ReadFileTemp ();
ReadFile testing = new ReadFile ();
something = testing.getLines();
//repaint (
//buf = testing.getBuf();
fileDat = testing.getData();
tempInt = testing.getInt ();
repaint ();
}
public void paint (Graphics screen)
{
Graphics2D screen2D = (Graphics2D) screen;
screen2D.drawString ("hello", 10, 20);
screen2D.drawString (something + "", 10, 50);
screen2D.drawString ((tempInt + ""), 10, 30);
screen2D.drawString (( fileDat + ""), 10, 80);
}
}
// end of SomethingMain.class
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//ReadFile.class
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class ReadFile extends javax.swing.JApplet //Applet
{
StringBuffer buf;
String FileToRead = "testFile.txt";
String fileData [] = new String [1];
int countLines = 2;
//--------------------------------------------------------
//public void init ()
//{
public ReadFile ()
{
String line;
URL url = null;
try
{
//System.out.println ("" + getCodeBase ());
url = new URL (getCodeBase (), FileToRead); // error
//url = new URL (codebase, FileToRead);
}
catch (MalformedURLException e)
{
System.out.println ("Malformed URL ");
stop ();
}
try
{
InputStream in = url.openStream ();
BufferedReader dis = new BufferedReader (new InputStreamReader (in)); // check number of lines
buf = new StringBuffer ();
countLines = 0;
while ((line = dis.readLine ()) != null)
{
countLines = countLines + 1;
}
in.close ();
dis.close ();
fileData = new String [countLines];
in = url.openStream ();
dis = new BufferedReader (new InputStreamReader (in)); // now read the file
for (int v = 0 ; v < countLines ; v++)
{
fileData [v] = dis.readLine ();
}
dis.close ();
}
catch (IOException e)
{
}
// Load the file into the TextArea.
//ta.append(buf.toString ());
repaint ();
}
int getLines ()
{
return countLines;
}
String getData ()
{
return fileData [1];
}
int getInt ()
{
return countLines;
}
}
// testFile.txt
banhkshkaj
hadsljaljdas
ashdfjskdahl
boomIf you can't see the error easily, please download the .zip. For, it contains everything needed to test it out.
__________________
Death smiles at us all. All a man can do is smile back. Last edited by Eric the Red; Jun 23rd, 2006 at 10:04 AM. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
I can't download and compile this at work (we only develop .NET), so what is the actual error message?
|
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Quote:
java.lang.NullPointerException
at java.applet.Applet.getCodeBase(Applet.java:136)
at ReadFile.<init>(ReadFile.java:27)
at MainSomething.init(MainSomething.java:16)
at ready.AppletRunner.run(AppletRunner.java:209)
at java.lang.Thread.run(Thread.java:536)
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Does anyone know? Anything would help me really. Maybe I'm not able to use "getCodeBase()" in a helper class. Does that sound realistic?
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#5 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
You could try overriding getCodeBase to return a fixed URL, or have the code base URL passed to it by its initiating class. You probably shouldn't inherit from JApplet for a helper class, either. It doesn't strike me as very modular design. |
|
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Quote:
Why would you say that inheritting from a JAPPLET for a helper class is wrong though? Please expand on that. It might prevent a lot of problems in the future.
__________________
Death smiles at us all. All a man can do is smile back. Last edited by Eric the Red; Jun 24th, 2006 at 10:12 PM. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|