Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 23rd, 2006, 9:47 AM   #1
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
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
boom


If you can't see the error easily, please download the .zip. For, it contains everything needed to test it out.
Attached Files
File Type: zip with helper doesn't work.zip (4.1 KB, 15 views)
File Type: zip without helper works.zip (2.6 KB, 16 views)
__________________
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.
Eric the Red is offline   Reply With Quote
Old Jun 23rd, 2006, 12:32 PM   #2
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
I can't download and compile this at work (we only develop .NET), so what is the actual error message?
jaeusm is offline   Reply With Quote
Old Jun 23rd, 2006, 1:15 PM   #3
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Quote:
Originally Posted by jaeusm
I can't download and compile this at work (we only develop .NET), so what is the actual error message?
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.
Eric the Red is offline   Reply With Quote
Old Jun 24th, 2006, 5:13 PM   #4
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
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.
Eric the Red is offline   Reply With Quote
Old Jun 24th, 2006, 6:24 PM   #5
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Eric the Red
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.
Presumably, the code base is supplied to an applet by the presiding JVM. If an applet is constructed manually, it doesn't appear to have access to the same information.

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.
Arevos is offline   Reply With Quote
Old Jun 24th, 2006, 10:01 PM   #6
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Quote:
Originally Posted by Arevos
Presumably, the code base is supplied to an applet by the presiding JVM. If an applet is constructed manually, it doesn't appear to have access to the same information.

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.
Thx, for your help!!! Okayy, I tried sending in the URL from the calling class and it now works.

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.
Eric the Red is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC