Link to home
Start Free TrialLog in
Avatar of akashsuresh
akashsureshFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Another piece of code on DOM seems to be giving a NullPointer Exception on <msg> tag

<logentry
   revision="1">
<date>2000-07-13T10:28:31.000000Z</date>
<paths>
<path
   action="A"
   kind="">/tags</path>
<path
   action="A"
   kind="">/trunk</path>
<path
   action="A"
   kind="">/branches</path>
</paths>
<msg>New repository initialized by cvs2svn.</msg>
</logentry>

Open in new window


For the above the snippet of code to parse is:
NodeList msglist=listEntryelement.getElementsByTagName("msg");
						Element msgelement=(Element)msglist.item(0);
						if(msgelement!=null){
						NodeList msgsublist=msgelement.getChildNodes();
						msg=((Node) msgsublist.item(0)).getNodeValue().trim();
						}
						else
						{
							msg="No Message";
						}

Open in new window


I am getting the exception at the line

msg=((Node) msgsublist.item(0)).getNodeValue().trim();
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You need to do the same you did for the outer Element:

NodeList msgsublist=msgelement.getChildNodes();
Element text = msgsublist.item(0);
if (text != null) {
  // Do it
}

Open in new window


I suspect there's only one text node though? If so, you might as well use hasChildNodes followed by getFirstChild
Avatar of akashsuresh

ASKER

Well for every log entry there is one message,So for 100 log entries,100 messages.

Can you explain your last statement.I am not quite proficient in DOM.

NodeList msglist=listEntryelement.getElementsByTagName("msg");
						Element msgelement=(Element)msglist.item(0);
						if(msgelement!=null){
						NodeList msgsublist=msgelement.getChildNodes();
						Element text=(Element) msgsublist.item(0);
						if(text!=null)
						{
						msg=((Node) text).getNodeValue().trim();
						}
						}
						else
						{
							msg="No Message";
						}

Open in new window



This gave me an error.
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
NodeList msglist=listEntryelement.getElementsByTagName("msg");
						Element msgelement=(Element)msglist.item(0);
						if(msgelement.hasChildNodes()){
						NodeList msgsublist=msgelement.getChildNodes();
						
						msg=((Node) msgsublist.item(0)).getFirstChild().getNodeValue().trim();
						
						}
						else
						{
							msg="No Message";
						}

Open in new window


Is this how you meant it to be?
It really depends on what kind of Node you're starting with. Using these DOM operation is hard work and highly error prone. You're doing yourself no favours by not using XPath. You could get all message text with

//msg/text()

Open in new window


in one go

http://technojeeves.com/joomla/index.php/free/63-xpath-tester
I understand that.However I have been working on this for a long time now and this is the last and final flaw to correct.I should have taken xpath from the very beginning but unfortunately I am already on the end of the road.

PathInfo path1=new PathInfo(null,null,null);
		List<LogProcess> logobject=new LinkedList<LogProcess>();
		List<PathInfo> pathinfo=new LinkedList<PathInfo>();
		List<PathInfo> alp4=new LinkedList<PathInfo>();
		ArrayList<String> pi10=new ArrayList<String>();
		
		try{
			
				DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
			
				DocumentBuilder db=dbf.newDocumentBuilder();
				Document d1=db.parse(fname);
				d1.getDocumentElement().normalize();
				
				NodeList logentrylist=d1.getElementsByTagName("logentry");
		
					Node n=logentrylist.item(0);
					Node att=n.getAttributes().getNamedItem("revision");
					
				NodeList list=d1.getElementsByTagName("author");
				
				int revision;
				Node temp,temp1;
				String author,date,msg = "None";
				al=new ArrayList<String>();
				
				for(int t=0;t<logentrylist.getLength();t++){
					Node list1=logentrylist.item(t);
						
					if(list1.getNodeType()==Node.ELEMENT_NODE)
					{
						Element listEntryelement=(Element)list1;
						Element le=(Element)logentrylist.item(t);
						
						NamedNodeMap nodelogmap=le.getAttributes();
						String rev=((Node)nodelogmap.item(0)).getNodeValue().trim();
						revision=Integer.parseInt(rev);
						NodeList authlist=listEntryelement.getElementsByTagName("author");
						Element authelement=(Element)authlist.item(0);
						if(authelement!=null){
						NodeList authsublist=authelement.getChildNodes();
						author=((Node) authsublist.item(0)).getNodeValue().trim();
						
						}
						else{
							author="Anonymous";
						}
						
						NodeList datelist=listEntryelement.getElementsByTagName("date");
						Element dateelement=(Element)datelist.item(0);
						NodeList datesublist=dateelement.getChildNodes();
						date=((Node) datesublist.item(0)).getNodeValue().trim();
						
												
						NodeList pathlist=listEntryelement.getElementsByTagName("path");
						
						
						pathinfo=new LinkedList<PathInfo>();
						for(int i=0;i<pathlist.getLength();i++)
						{//System.out.println(pathlist.getLength());
							Element pe=(Element)pathlist.item(i);
							
							NamedNodeMap peattr=pe.getAttributes();
							temp=peattr.item(0);
							temp1=peattr.item(1);
							
							path1=new PathInfo(temp.getNodeValue(),temp1.getNodeValue(),pe.getTextContent());
							pathinfo.add(path1);
							
							
						}
						
						NodeList msglist=listEntryelement.getElementsByTagName("msg");
						Element msgelement=(Element)msglist.item(0);
						if(msgelement.hasChildNodes()){
						NodeList msgsublist=msgelement.getChildNodes();
						
						msg=((Node) msgsublist.item(0)).getFirstChild().getNodeValue().trim();
						
						}
						else
						{
							msg="No Message";
						}
						LogProcess lp=new LogProcess(revision,author,date,pathinfo,msg);
						logobject.add(lp);
					
					}
						//System.out.println(pathinfo.size());	
						
						/*for(LogProcess lp3:logobject)
						{
							for(List<PathInfo> pi3:pathinfo)
							{
							  for(PathInfo pi7:pathinf)
							  {
								System.out.println(pi7.getFilepath());
							  }
							}
						}*

Open in new window


I would really appreciate if you could help me sort this out.

If you notice in the above code "datetist" works fine the same way,but msglist seems to mess up
You'd be better off just getting the <msg> element directly off each <logentry>
I tried that already.does not work.
Back later
I found that the exception comes at the places where the tag shows <msg></msg>
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are a life saver.It works for me as well.Thank you so much
:)
How large is your log file btw?