Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Reflection - null pointer

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reflection - null pointer

    Well this is a long shot, but I hopefully someone here can help. The goal is to use Introspection and Reflection. In short, the program creates a JFrame, left side is another JFrame and it displays an object, Rect, Circ or Ticker. Rigth side has a dropdown menu where you select which object is displayed. Under it (still right side) there are 2 more JFrames. One with the property names and the other with the property values of whatever is displayed (rect, circ or ticket). See picture below.



    When an item in a text field is changed, it is supposed to redraw the object. However, I get null pointer exception. See here. Lines 336 and 183 are problematic, but I can't see why.

    The following classes are present: Rect, Circ, Ticker, RectBeanInfo, CircBeanInfo, TickerBeanInfo, JFrameExt (the testing class).

    I've tried to rename the jtfActionPerformed to just a general actionPerfomed, didn't help.

    Click here for JFrameExt.

    Rect, Circ and Ticker are identical so I will only include one of them in case you need.

    Click here for Rect.

    Click here for RectBeanInfo.

    This is the last part that I need to fix, the reflection of the changes. Please advise if you can in any way.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Reflection - null pointer

    Why not use a JSplitPanel or something like that?

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reflection - null pointer

    Instructions are to use separate panels... tbh I would do the whole thing differently, but the point is to learn about reflection and introspection...

  4. #4
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Reflection - null pointer

    Can you post the stack trace from the NPE?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Reflection - null pointer

    First, I'd suggest boiling that code down to a smaller version which demonstrates the problem and posting the code and errors here. While the lines don't match up, I do not see any code which intializes the PropertyDescriptor array - which would equate to a NullPointerException.

  6. #6
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reflection - null pointer

    Quote Originally Posted by DavidFongs
    Can you post the stack trace from the NPE?
    I posted it as a link, but here, once more.

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at intro.JFrameExt.jtfActionPerformed(JFrameExt.java:336)
    	at intro.JFrameExt$3.actionPerformed(JFrameExt.java:183)
    	at javax.swing.JTextField.fireActionPerformed(Unknown Source)
    	at javax.swing.JTextField.postActionEvent(Unknown Source)
    	at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
    	at javax.swing.SwingUtilities.notifyAction(Unknown Source)
    	at javax.swing.JComponent.processKeyBinding(Unknown Source)
    	at javax.swing.JComponent.processKeyBindings(Unknown Source)
    	at javax.swing.JComponent.processKeyEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    	at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

    Line numbers do match. Here is first one

    179:  //Register this as the listener with JTextField object
    180:  jtfPropValues[j].addActionListener(new java.awt.event.ActionListener() {
    181:  	public void actionPerformed(java.awt.event.ActionEvent e) {
    182:  	// reflect changes
    183:  	jtfActionPerformed(e);
    184:  	}
    185:  });

    Here is second one
    331:  //Get the set method object.
    332:  Method mset = pd[i].getWriteMethod();
    333:
    334:  //Invoke set method and pass it target bean and parameters.
    335:  try {
    336:  	mset.invoke(targetBeanObject, params);
    337:  }

    And here is the jtfActionPerformed
    /** text field actionListener event begin*/
    	public void jtfActionPerformed(ActionEvent e) {
    		int i;
    	    String propName="", propValue="";
     
    	    //Determine the name, value and index of the property that changed.
    	    for (i=0; i<jtfPropValues.length;i++) {
    	    	if (e.getSource()== jtfPropValues[i]) {
    	    		break;
    	        }
    	    }
     
    	    //Get the property name and the property value from the
    	    //JLabel and JTextfield corresponding to the i value.
    	    propName = jlbPropNames[i].getName();
    	    propValue = jtfPropValues[i].getText();
     
    	    //Note that the property index for the above property in the pd
    	    //array is the same. So you can go to that index in pd array and
    	    //access its property type .
    	    Class propType = pd[i].getPropertyType();
     
    	    //Get the property type as a String
    	    String propTypeName = propType.getName();
     
     
    	    //Create Object array for storing parameters
    	    Object[] params = new Object[1];
     
    	    //Depending upon property name, create correct parameter object.
    	    if (propTypeName.equals("int")) {
    	    	params[0] = new Integer(Integer.parseInt(propValue));
    	    }
     
    	    else if (propTypeName.equals("double")) {
    	    	params[0] = new Double(Double.parseDouble(propValue));
    	    }
     
    	    else if (propTypeName.equals("boolean")) {
    	        params[0] = new Boolean(propValue);
    	    }
     
    	    else if (propTypeName.equals("java.lang.String")) {
    	        params[0] = propValue;
    	    }
     
    	    //Get the set method object.
    	    Method mset = pd[i].getWriteMethod();
     
    	    //Invoke set method and pass it target bean and parameters.
    	    try {
    	    	mset.invoke(targetBeanObject, params);
    	    }
     
    	    catch (IllegalAccessException ex) {
    	    }
     
    	    catch (IllegalArgumentException ex) {
    	    }
     
    	    catch (InvocationTargetException ex) {
    	    }
     
    	}
    	/* actionListener event end */

    Quote Originally Posted by Copeg
    I do not see any code which intializes the PropertyDescriptor array - which would equate to a NullPointerException.
    Line 34:
    private PropertyDescriptor[] pd = null;

    Then 250-251
    //Get an array of PropertyDescriptor objects from BeanInfo object.
    pd = bi.getPropertyDescriptors();
    Last edited by bgalakazam; December 3rd, 2010 at 06:58 PM.

Similar Threads

  1. Null Pointer Exception Help!!
    By puzzledstudent in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 11th, 2010, 06:46 PM
  2. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM
  3. HttpURLConnection null pointer exception
    By chopficaro in forum Java Theory & Questions
    Replies: 0
    Last Post: May 10th, 2010, 10:19 AM
  4. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM