[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Exception with null

Posted on 2004-11-07
49
Medium Priority
?
283 Views
Last Modified: 2010-03-31
Hi,

I have some basic question. Please assist me asap.

I have 2 programs which are found in the same package structure. The first program
with the method getDataTagValue uses the 2nd program UtilException.

But the problem when the First program uses Utilexception it does not display the message as mentioned
"tagName [" + tagName + "] cannot be retrieved"
instead it throws null;


First Program
========
    public static synchronized String getDataTagValue( String  tagName,
                                                       Element dataElement
                                                     )
                                                    throws UtilException {
       
        try{
           
            NodeList list = dataElement.getElementsByTagName(tagName);
            String value = list.item(0).getFirstChild().getNodeValue();
            return value.trim();
           
        }catch(Exception ex){
           
            ex.printStackTrace();
            throw new UtilException("tagName [" + tagName + "] cannot be retrieved");

        }
    }


Second Program
==========
public class UtilException extends Exception{


    private String Message;

    public UtilException(){
        super();
    }
   
    public UtilException(String Message) {
        super(Message);
    }
   
    public void setMessage(String Message){
        this.Message = Message;
    }
   
    public String getMessage(){
        return Message;
    }

}

Can anyone assist what changes needs to be made with my utilexception program,
which can display the exact message rather than showing null.

Thanks
bangviz
0
Comment
Question by:bangviz
  • 20
  • 19
  • 7
  • +2
49 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 90 total points
ID: 12519675
public class UtilException extends Exception{
    public UtilException(){
        super();
    }
   
    public UtilException(String Message) {
        super(Message);
    }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12519728
The reason you have a null pointer exception is you have an anomaly in your code: you don't actually initialize 'Message', you initialize 'message' as you let the superclass do it. It initializes the latter, leaving the former unitialized
0
 
LVL 1

Author Comment

by:bangviz
ID: 12519910
Hi,

I made changes as mentioned as below and I tried running a testcase on this and I still get the value as null.


public class UtilException extends Exception{

    public UtilException(){
        super();
    }
   
    public UtilException(String Message) {
        super(Message);
    }

}



public class TestException extends TestCase{
   
    public TestException(String arg0) {
        super(arg0);
    }

    public static Test suite() {

        TestSuite  suite = new TestSuite("TestException");

        suite.addTest( new testHarness( "testException") );

        return suite;
    }
   
    public void testException() throws Exception{
       
        UtilException cue = new UtilException();
       
        String ErrorMessage = "Test this";

        System.out.println("Error:" + cue.getMessage());
             
    }  

}

Any ideas.

Thanks
Bangviz
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 12519921
You don't give the exception a message:

>>UtilException cue = new UtilException();

0
 
LVL 92

Expert Comment

by:objects
ID: 12519927
> The reason you have a null pointer exception is you have an anomaly in your code

No NPE was mentioned (nor would one occur).

The problem with the code is that you overide the getMessage() method from the super class.
The Exception class already has support for an error message, so it is not needed.
0
 
LVL 92

Expert Comment

by:objects
ID: 12519937
try this:

        String ErrorMessage = "Test this";
        UtilException cue = new UtilException(ErrorMessage);
        System.out.println("Error:" + cue.getMessage());
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12519938
>>No NPE was mentioned (nor would one occur).

I meant null, not an exception

>>
The problem with the code is that you overide the getMessage() method from the super class.
The Exception class already has support for an error message, so it is not needed.
>>

(Already explained)
0
 
LVL 92

Expert Comment

by:objects
ID: 12519955
> I meant null, not an exception

Well it what you said that counts.
Lucky I corrected you :)

> (Already explained)

You talked about initializing Message which had nothing to do with it, and didn't mention anything about the superclass.
In fact Message is no longer needed as I explained.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12519968
>>You talked about initializing Message which had nothing to do with it

It has everything to do with, since that's the value that's not being initialized and that's the value getting returned by getMessage
0
 
LVL 92

Expert Comment

by:objects
ID: 12519994
bangviz,

Try the code I posted, and let me know if you have any questions.

Additionally if you'l like a default message to be used when one isn't specified use the following:

public class UtilException extends Exception{
    public UtilException(){
        this("Util error");
    }
   
    public UtilException(String Message) {
        super(Message);
    }
}
0
 
LVL 1

Author Comment

by:bangviz
ID: 12520040
Objects,

I tried ur code
try this:

        String ErrorMessage = "Test this";
        UtilException cue = new UtilException(ErrorMessage);
        System.out.println("Error:" + cue.getMessage());

But when i run the output i get is Error:null

I'm still not getting the value "Test this";
0
 
LVL 92

Expert Comment

by:objects
ID: 12520043
did u change your exception class as I posted above?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 60 total points
ID: 12520063
Simplify your code to test it:


public class UtilException extends Exception{

            public UtilException(){
                        super();
            }

            public UtilException(String Message) {
                        super(Message);
            }

            public static void main(String[] args) throws UtilException {
                  throw new UtilException("Testing ...");
            }

}
0
 
LVL 92

Expert Comment

by:objects
ID: 12520067
Theres nothing wrong with the code currently being used to test it.
0
 
LVL 92

Expert Comment

by:objects
ID: 12520072
And adding a main to a class just for testing it is not good practice.
Your current test driver is a far better approach.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12520073
Code that's not wrong usually works doesn't it?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12520079
>>And adding a main to a class just for testing it is not good practice.

bangviz, as it happens, it's perfectly OK. Once it's tested you get rid of main
0
 
LVL 92

Expert Comment

by:objects
ID: 12520096
> Code that's not wrong usually works doesn't it?

Please read the earlier comments if you don't understand what the problem is.
Creating an extra redundant test driver will make absolutely no difference.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12520103
>>if you don't understand what the problem is.

I understand precisely what the problem is thank you ..;-)
0
 
LVL 1

Author Comment

by:bangviz
ID: 12520108
Hi,

I made changes as adviced but my test shows Error:null,does not print the value.


public class UtilException extends Exception{

    public UtilException(){
        super();
    }
   
    public UtilException(String Message) {
        super(Message);
    }

//Added this for testing    
public static void main (String[]args) throws CIUtilException{
       
        throw new CIUtilException("Testing");
    }


}



public class TestException extends TestCase{
   
    public TestException(String arg0) {
        super(arg0);
    }

    public static Test suite() {

        TestSuite  suite = new TestSuite("TestException");

        suite.addTest( new testHarness( "testException") );

        return suite;
    }
   
    public void testException() throws Exception{
       
        String ErrorMessage = "Test this";

        UtilException cue = new UtilException(ErrorMessage);

        System.out.println("Error:" + cue.getMessage());
             
    }  

}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12520114
If you run just the code i last posted, you'll get:

Exception in thread "main" UtilException: Testing ...
        at UtilException.main(UtilException.java:12)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12520130
>>If you run just the code i last posted

To do that, put it in a new directory and do


set classpath=.
javac UtilException.java
java UtilException
0
 
LVL 92

Expert Comment

by:objects
ID: 12520201
Check that you recompiled UtilException and that there isn't another UtilException class in your classpath.
0
 
LVL 1

Author Comment

by:bangviz
ID: 12520240
nope not yet...its value=null
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12520249
If you follow my last comments, you can't fail to get the same result as i did
0
 
LVL 92

Expert Comment

by:objects
ID: 12520255
> If you follow my last comments, you can't fail to get the same result as i did

Obviously thats not the case :-D
0
 
LVL 1

Author Comment

by:bangviz
ID: 12520258
i tried as u posted and its

public class UtilException extends Exception{

    public UtilException(){
        super();
    }
   
    public UtilException(String Message) {
        super(Message);
    }

//For test only
    public static void main (String[]args) throws CIUtilException{
       
        throw new CIUtilException("Testing");
    }


}



public class TestException extends TestCase{
   
    public TestException(String arg0) {
        super(arg0);
    }

    public static Test suite() {

        TestSuite  suite = new TestSuite("TestException");

        suite.addTest( new testHarness( "testException") );

        return suite;
    }
   
    public void testException() throws Exception{
       
        String ErrorMessage = "Test this";

        UtilException cue = new UtilException(ErrorMessage);

        System.out.println("Error:" + cue.getMessage());
             
    }  

}
0
 
LVL 92

Expert Comment

by:objects
ID: 12520259
> nope not yet...its value=null

The problem is not with your code.
It looks like you are running an old version of UtilException, make sure you have recompiled it and are using the latest version.
0
 
LVL 92

Expert Comment

by:objects
ID: 12520264
that code won't actually compile (due to the main() you added), which is probably why you are seeing what you are as I posted above
Remove the main from UtilException and recompile it. Then try running your test driver.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12520266
>>Obviously thats not the case :-D

That's because it appears it has *not* been tried:

>>i tried as u posted and its

(there follows not the code i posted)
 
0
 
LVL 92

Expert Comment

by:objects
ID: 12520270
public class UtilException extends Exception{

    public UtilException(){
        this("Util Error");
    }
   
    public UtilException(String Message) {
        super(Message);
    }
}



public class TestException extends TestCase{
   
    public TestException(String arg0) {
        super(arg0);
    }

    public static Test suite() {

        TestSuite  suite = new TestSuite("TestException");

        suite.addTest( new testHarness( "testException") );

        return suite;
    }
   
    public void testException() throws Exception{
       
        String ErrorMessage = "Test this";

        UtilException cue = new UtilException(ErrorMessage);

        System.out.println("Error:" + cue.getMessage());
             
    }  

}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12520278
>>
throws CIUtilException{
       
        throw new CIUtilException("Testing");
>>

is of course completely different to what i posted
0
 
LVL 1

Author Comment

by:bangviz
ID: 12520400
sorry for the confusion its not ciutilexception.
its utilexception.

1) Anyway i removed main method and compiled utilexception and ran the test its value ie Error:null

2) I added the main and compiled utilexception and ran the test its value ie Error:null

Its still not printing the value which is been passed. Is something wrong with utilexception or my testcase?
0
 
LVL 92

Expert Comment

by:objects
ID: 12520404
theres nothing wrong with your code that i can see.
i suspect thers an old class file in your classpath somewhere.

check your classpath and delete any UtilException.class files you find, then recompile and test.
0
 
LVL 1

Author Comment

by:bangviz
ID: 12520478
Hi,

The final code looks like this after recompiling once again,checking my classpaths and running my testcase.
The value is still null, Error:null.

Its pretty strange why I'm unable to get the value's passed.


public class UtilException extends Exception{

    public UtilException(){
        super();
    }
   
    public UtilException(String Message) {
        super(Message);
    }

}


import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class TestException extends TestCase{
   
    public TestException(String arg0) {
        super(arg0);
    }

    public static Test suite() {

        TestSuite  suite = new TestSuite("TestException");

        suite.addTest( new testHarness( "testException") );

        return suite;
    }
   
    public void testException() throws Exception{
       
        String ErrorMessage = "Test this";

        UtilException cue = new UtilException(ErrorMessage);

        System.out.println("Error:" + cue.getMessage());
             
    }  

}


Thanks
Bangviz
0
 
LVL 92

Expert Comment

by:objects
ID: 12520686
make the following change to your exception class and run it, and let me know if the println() message gets displayed:

public class UtilException extends Exception{

    public UtilException(){
        super();
    }
   
    public UtilException(String Message) {
        super(Message);
        System.out.println("UtilException ctor called "+Message);
    }

}
0
 

Expert Comment

by:engineer007
ID: 12521108
Here is the modification to your original code you ll need to incorporate to get the actual exception.

 public static synchronized String getDataTagValue( String  tagName,
                                                       Element dataElement
                                                     )
                                                    throws UtilException {
       
        try{
           
            NodeList list = dataElement.getElementsByTagName(tagName);
            String value = list.item(0).getFirstChild().getNodeValue();
            return value.trim();
           
        }catch(Exception ex){

        if (ex instanceof NullPointerException) {
            String message = "NullPointerException==>";
            for (int i = 0; i < ex.getStackTrace().length; i++) {
                message += ex.getStackTrace()[i].getClassName() + "." +
                    ex.getStackTrace()[i].getMethodName() + "." +
                    ex.getStackTrace()[i].getLineNumber();
            }
           System.out.println(message);
        }
           else{
            ex.printStackTrace();
           }
            throw new UtilException("tagName [" + tagName + "] cannot be retrieved");

        }
    }


0
 

Expert Comment

by:awt_of_control
ID: 12521954
Your first program is coded to deliver a UtilException but in your first prog's method code, you catch an Exception (ex) instead. You know this works, because you ask for the stack trace. Then you go and throw a new UtilException, and as CEHJ says, there's no Message in it - so that's where your Null comes from.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12522045
If you follow my last comments to the letter (below), you can't go wrong. Your test harness in its current form is not quite right anyway, as you should throw an Exception to test it realistically - at the moment you're just trying to display one of its properties, so it may just as well be any other class. Get the simplest case working first though, using the following:

====================================================================

Simplify your code to test it:


public class UtilException extends Exception{

          public UtilException(){
                    super();
          }

          public UtilException(String Message) {
                    super(Message);
          }

          public static void main(String[] args) throws UtilException {
               throw new UtilException("Testing ...");
          }

}
====================================================================

>>If you run just the code i last posted

To do that, put it in a new directory and do


set classpath=.
javac UtilException.java
java UtilException

====================================================================
0
 

Expert Comment

by:awt_of_control
ID: 12522218
CEHJ - your class thorws an Exception itself at line 12.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12522240
Yes - it's meant to (that's the test) ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12528292
> Your test harness in its current form is not quite right anyway, as you should throw an Exception to test it realistically

not at all necessary, there absolutely no need

> - at the moment you're just trying to display one of its properties

thats what its testing !

> Yes - it's meant to (that's the test) ;-)

What needs to be tested is returning the message from the exception, not that it can be thrown.
We all know exceptions can be thrown.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12528309
>>
What needs to be tested is returning the message from the exception, not that it can be thrown.
We all know exceptions can be thrown.
>>

That's what the code i posted does
0
 
LVL 92

Expert Comment

by:objects
ID: 12528338
No thats what the test case does.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12528359
My code does both ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12528393
So whats wrong with the existing test code?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12528407
>>So whats wrong with the existing test code?

It's adds unnecessary complexity at a stage when the class in question is not running as it should
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12554150
8-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12560666
Good to hear my suggestion fixed the problem :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question