Solved

add(Component) method question

Posted on 2001-06-06
10
189 Views
Last Modified: 2010-03-31

In the init() method, I use add(myLabel). If myLabel is null, we get a nullPointer exception. Where is the method that throws that exception? I could not find it in the Component class where the add methods are written.
0
Comment
Question by:HanRui
  • 6
  • 3
10 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
It's in the Applet class or one of it descendants, probably Container.
Why do you need to know?

What you are doing is invalid and that is why you are getting an exception.
0
 

Author Comment

by:HanRui
Comment Utility
I looked in the classes all the way up the hierarchy, and there is no add method that is defined with "throws exception." I want to understand this so that I can communicate it to students. If an exception is reported, there must be a method somewhere that throws it.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Hang on I'll find it for you.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Sorry, it's too early in the morning.
The exception won't be throw explicitly, it'll be thrown when you try and do something like this:

Object a = null;
String s = a.toString()  // This will cause a NPE to be thrown

I'll find the spot in the code where its occurring for you.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
Comment Utility
From the NPE javadoc:

Thrown when an application attempts to use null in a case where an object is required. These include:

- Calling the instance method of a null object.
- Accessing or modifying the field of a null object.
- Taking the length of null as if it were an array.
- Accessing or modifying the slots of null as if it were an array.
- Throwing null as if it were a Throwable value.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 92

Expert Comment

by:objects
Comment Utility
It's thrown from the addImpl method in the Container class.

Another easy way to find where an exception originated is to use the printStackTrace() method of the Exception class.

eg.

tr
{
   ....
}
catch (Exception e)
{
   e.printStackTrace();
}
0
 

Author Comment

by:HanRui
Comment Utility
Ok. I still want to see a method somewhere with throws exception in it. The priniciples that you listed make sense, but the more general principle is that for an exception to be thrown at all, there must be some method somewhere with "throws exception" as part of its definition.

java.lang.NullPointerException
  at java.awt.Container.addImpl(Compiled Code)
  at java.awt.Container.add(Compiled Code)
  at Test1.init(Compiled Code)
* at netscape.applet.DerivedAppletFrame$InitAppletEvent.dispatch(Compiled Code)
  at java.awt.EventDispatchThread$EventPump.dispatchEvents(Compiled Code)
  at java.awt.EventDispatchThread.run(Compiled Code)
  at netscape.applet.DerivedAppletFrame$AppletEventDispatchThread.run(Compiled Code)

The above is the message output in the Java console. The addImpl method does not throw an exception. I did see an IllegalArgumentException in the Container class, but no method with throws exception.
0
 
LVL 7

Expert Comment

by:Igor Bazarny
Comment Utility
Hi,

There is no need to declare 'throws Exception' to throw NulPointerException. Exception derived from RuntimeException end Error can be thrown from anywhere, no matter what throws clause says.
add() is method family of Container (not Component) and real implementation is in addImpl(). It's no wonder that it throws NullPointerException when you add null component. In fact, it prevents you from more serious problems--ability to add null component would possibly cause NullPointerException far from call to add().

You would better find a Label to add. And watch out, from my experience, NullPointerException is most often exception, and most helpful--you can tell from stacktrace where you try to use uninitialized reference. Much better than simply fall with 'General Protection Fault' message.

Regards,
Igor Bazarny
Brainbench MVP for Java 1
www.brainbench.com
0
 

Author Comment

by:HanRui
Comment Utility
Thank you. I already closed the question, but I appreciate your feedback.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
The exception is thrown by the JVM as a result of calling a method on a null object reference.

Thanks for the points :)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Exchange and Third Party application. 8 59
Increment alphanumeric sequence 6 79
factorial example challenge 10 61
endX challenge 2 48
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now