Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2007

Tip: Looking for answers? Try searching our database.

I get a null pointer when running my applet

Thread view: 
Justin - 16 Jan 2007 04:43 GMT
I have a class that extends a JApplet.  It runs fine when I run the
program from within netbeans, however, when I attempt to run it from
the netbeans generated .html file, I get a null pointer exception.

I traced the null pointer to a database connection class, the error
occurs when the program closes the connection to the database.  Any
ideas on how to fix this and why it is occuring?
Andrew Thompson - 16 Jan 2007 05:19 GMT
> I have a class that extends a JApplet.  It runs fine when I run the
> program from within netbeans, however, when I attempt to run it from
[quoted text clipped - 3 lines]
> occurs when the program closes the connection to the database.  Any
> ideas on how to fix this and why it is occuring?

It possibly is restricted from doing some action in
the default security envirnonment of a browser that
is permitted in the applet viewer.  OTOH, such a thing
is much more likely to occur when loading the DB
access class(es or driver), or connecting, than when
*closing* an active connection.

Do you have a code example that demonstrates
the problem?

Andrew T.
Justin - 16 Jan 2007 19:08 GMT
Heres two of my classes.  The first one is the applet class.  It
creates a DataConnection class.  The DataConnection class queries an
access database.

//**************
//Applet class
//**************

package consultviewer;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Main2 extends javax.swing.JApplet{

   JPanel jpan;
   Panel panel;

   int width = 794;

   public void init(){
    setSize(width, 500);
   }

   public Main2(){
    DataConnection dc = new DataConnection();

    DefaultListModel testing = dc.queryConnection("SELECT * FROM
newconsult;");
    System.out.println(testing);
   }
}

//**********
//DataConnection Class
//**********

package consultviewer;

import java.sql.*;
import javax.swing.DefaultListModel;
import javax.swing.*;

class DataConnection
      {

   String filename = "C:\\Documents and Settings\\Owner\\Consult.mdb";

   public DefaultListModel queryConnection(String sql){

       sql = sql.replace('"', '\'');
       Connection connection = null; // manages connection
       Statement statement = null; // query statement
       DefaultListModel returnedResults = null;
       // connect to database books and query database
       try {
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
           String database = "jdbc:odbc:Driver={Microsoft Access
Driver (*.mdb)};DBQ=";
           database+= filename.trim() + ";DriverID=22;READONLY=true}";
// add on to the end
           // establish connection to database
           connection = DriverManager.getConnection( database ,"","");

           // create Statement for querying database
           statement = connection.createStatement();
           // query database

           ResultSet resultSet = statement.executeQuery(sql);

           // process query results
           ResultSetMetaData metaData = resultSet.getMetaData();
           int numberOfColumns = metaData.getColumnCount();

           returnedResults = new DefaultListModel();

           while ( resultSet.next()){
               for ( int i = 1; i <= numberOfColumns; i++ ){
                   returnedResults.addElement(resultSet.getObject(i));
               }
           } // end while

       }  // end try

       catch ( SQLException sqlException ) {
           sqlException.printStackTrace();
           System.exit( 1 );
       } // end catch
       catch ( ClassNotFoundException classNotFound ) {
           classNotFound.printStackTrace();
           System.exit( 1 );
       } // end catch
       finally // ensure statement and connection are closed properly
       {
           try {
               statement.close();
               connection.close();
           } // end try
           catch ( Exception exception ) {
        System.out.println("it failed here");
               exception.printStackTrace();
               System.exit( 1 );
           } // end catch
       } // end finally
       return returnedResults;
   }
}
Andrew Thompson - 17 Jan 2007 02:18 GMT
> Heres two of my classes.

OK - the first thing I notice is..
....
>         catch ( SQLException sqlException ) {
>             sqlException.printStackTrace();
>             System.exit( 1 );
>         } // end catch

..the System.exit(1)

An applet in a web page might share a VM with
other applets on the page, or applets in other
frames, or potentially even other instances of the
same browser.

If it were allowed to call System.exit(aNumber)
that could cause problems for other applets, so
they can't - calling exit should result in security
or general 'no' exceptions - even in a signed/trusted
applet.

As an aside, had I asked you why you're developing
an applet, specifically?  (What needs do you see the
applet filling, that could not be done by an application?)

Andrew T.
Justin - 17 Jan 2007 02:45 GMT
Well, I am uber new to java, and I work in a hospital and I am
designing a consult ordering system for our psych department.  I
figured an applet would be the easiest way to provide access to the
same program all over the facility.  I was planning on putting it on an
Intranet site to avoid any security issues.  Then I have the access
database in a shared drive on the network.

I am an industrial engineer by trade, and I'm not as much interested in
producing the greatest possible ordering system (not saying I'm not
trying to write it) but I am more interested in the statistics that
this form generates.  I'll use them for building statistical models
later.

And actually, up until a few hours ago, I did not realize that I could
make an executable .jar file which I think is what I am going to do.
And then copy that file onto the desktop of any computer that will use
the program.
Andrew Thompson - 17 Jan 2007 03:56 GMT
> Well, I am uber new to java,

Don't worry - you are making good progress.
(But please use words like "'very' new", so that even
an e.g. *Aussie* can understand what you mean)

(big snip..)
> And actually, up until a few hours ago, I did not realize that I could
> make an executable .jar file which I think is what I am going to do.

You are heading in just the right direction to make
this much better for your users, and much easier to
deploy and ..update, but..

> And then copy that file onto the desktop of any computer that will use
> the program.

No!  This application and deployment is almost *made*
for deployment as an application (like you are making
in the executable .jar file) but *deployed/launched*
by Java Web Start - direct from the network.

Have a look over some of these web start examples
to get a look and feel for how easy it is for the user ..
<http://www.physci.org/jws/>
..most of these are sandboxed, but they can also
prompt for extended permissions, if needed.

Umm.. oh yeah.  Note that we have gone somewhat
off on a tangent on this sub-thread, and at this instant,
I have no revelations about your NPE, though I intend
to review the thread and see if I can spot anything..

There *might* be something wrong with the DB
access itself, but I am no DB expert, and those
folks tend to frequent either c.l.j.programmer or
c.l.j.databases.

Would you accept my advice to widen the post
to one or the other of those groups?

If you think that is a good idea, please mention
right here, on this thread, rather than try it yourself.

Widening a post can be taken badly, and
it needs to be handled just the right way.
..If it is going to have a good chance not to
make people irritable!

Andrew T.
Justin - 17 Jan 2007 05:21 GMT
Well, since I've changed it to an application instead of an app, I am
no longer getting the NPE.  So problem solved I guess???  I mean, if
turning this into an Application rather than an Applet is the way to
go....

> > Well, I am uber new to java,
>
[quoted text clipped - 46 lines]
>
> Andrew T.
Andrew Thompson - 17 Jan 2007 06:13 GMT
> Well, since I've changed it to an application instead of an app, I am
> no longer getting the NPE.  So problem solved I guess???

Great!  Glad it's ..sorted.  (In a way it is, but it
would be nice to track down exactly why it
failed as an applet..)

Could I ask of you, though, that you put replies
immediately *after* the text you are replying to*,
rather than 'on-top' as you are currently doing?
(* and excellent if you can then trim any text
not relevant to your reply.)

>.. I mean, if
> turning this into an Application rather than an Applet is the way to
> go....

It sure is, but don't forget about web start,
because it will both help deploy the application,
and make any updates very easy.

That URL of web-start examples, again..
> > <http://www.physci.org/jws/>

HTH

Andrew T.
Justin - 17 Jan 2007 07:00 GMT
> Could I ask of you, though, that you put replies
> immediately *after* the text you are replying to*,
> rather than 'on-top' as you are currently doing?
> (* and excellent if you can then trim any text
> not relevant to your reply.)

How's this for practice???

> It sure is, but don't forget about web start,
> because it will both help deploy the application,
> and make any updates very easy.
>
> That URL of web-start examples, again..
> > > <http://www.physci.org/jws/>

Lol, looks like something else I'm going to have to deal with another
day.  Thanks for the help
Andrew Thompson - 17 Jan 2007 07:05 GMT
...
> > ..don't forget about web start,
...
> Lol, looks like something else I'm going to have to deal with another
> day.

Cool.  It pays not to take on too much
at any one moment.

>...Thanks for the help

You're welcome.

Andrew T.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.