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 / General / April 2007

Tip: Looking for answers? Try searching our database.

nullpointer exception while try to retrieve the elements from the array object

Thread view: 
rajachezhian@gmail.com - 14 Apr 2007 13:11 GMT
hi,
i had created the array object by using getter/setter method.while i
try to retrieve the elements from the array object. null pointer
exception raise in line no : 35 and line no: 64 .Any body help me to
over come my problem.here's my coding

//Sample .java for Menu to get List Object to a Array
//getter/setter method is in Menu.java

package example;

import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.Document;
import org.jdom.Element;
import java.util.*;
import java.io.*;

public class  ParseXml
{

    public static void main(String[] args)
    {
        Menu mainMenu = new Menu();
        try{
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build( new File("C:/Application/
Specification/ApplicationMenu.xml"));
            Element root = ( Element)doc.getRootElement();
            List listroot = root.getChildren();
            Iterator itr = listroot.iterator();
            while( itr.hasNext()){
                Element nextChild = ( Element)itr.next();
   
mainMenu.setDescription( nextChild.getAttribute( "description").getValue());
                mainMenu.setMenuID( nextChild.getAttribute( "id").getValue());
                getConsolidateMenu( mainMenu, nextChild, 0);
            }
        }catch( Exception e){
            e.printStackTrace();
        }
        printMenu( mainMenu);
    }
    public static void getConsolidateMenu( Menu menu, Element current,
int depth){
        printSpaces( depth);
   
System.out.println( current.getAttribute( "description").getValue());
        List list = current.getChildren();
        Iterator it = list.iterator();
        Menu mainMenu = new Menu();
        Menu [] subMenu = new Menu[list.size()];
        int i = 0;
        while( it.hasNext()){
            Element child = (Element)it.next();
   
mainMenu.setDescription( child.getAttribute( "description").getValue());
            mainMenu.setMenuID( child.getAttribute( "id").getValue());
            subMenu[i] = new Menu();
   
subMenu[i].setDescription(child.getAttribute( "description").getValue());
            subMenu[i].setMenuID( child.getAttribute( "id").getValue());
            mainMenu.setSubMenu( subMenu);
            getConsolidateMenu( mainMenu, child, depth + 1);
            i++;
        }
    }
    public static void printSpaces( int n){
        for( int i = 0; i <= n; i++){
            System.out.print( " ");
        }
    }
    public static void printMenu( Menu menu){
        Menu mainMenu = new Menu();
        Menu [] subMenu = mainMenu.getSubMenu();
        for( int i = 0; i < subMenu.length; i++){
            System.out.println( subMenu[i]);
        }
    }
}
//Menu Class for ParseXml.java getter/setter method

package example;

public class  Menu
{
    String menuID;
    String description;
    Boolean isService;
    String serviceID;
    Menu [] subMenu;
    public void setMenuID( String menuID){
        this.menuID = menuID;
    }
    public String getMenuID(){
        return menuID;
    }
    public void setDescription( String description){
        this.description = description;
    }
    public String getDescription(){
        return description;
    }
    public void setIsService( Boolean isService){
        this.isService = isService;
    }
    public Boolean getIsService(){
        return isService;
    }
    public void setServiceID( String serviceID){
        this.serviceID = serviceID;
    }
    public String getServiceID(){
        return serviceID;
    }
    public void setSubMenu( Menu [] subMenu){
        this.subMenu = subMenu;
    }
    public Menu[] getSubMenu(){
        return subMenu;
    }
}
Thanks in advance,

cheers,
raja
Patricia Shanahan - 14 Apr 2007 14:02 GMT
> hi,
> i had created the array object by using getter/setter method.while i
> try to retrieve the elements from the array object. null pointer
> exception raise in line no : 35 and line no: 64 .Any body help me to
> over come my problem.here's my coding
...

I have some notes on how to debug at
http://home.earthlink.net/~patricia_shanahan/debug/

In your case I would start with two competing theories, and try to
construct tests to see if either is true:

1. An array access was attempted before the set method was called.

2. The set method was called, but the last time it was called before the
failing access its argument was null.

Patricia
rajachezhian@gmail.com - 16 Apr 2007 05:57 GMT
> rajachezh...@gmail.com wrote:
> > hi,
[quoted text clipped - 16 lines]
>
> Patricia

hello patricia,
Thanks for your reply,i will try it.

with thanks,
raja


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.