473,480 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

dereferencing null pointer exception

oll3i
679 Contributor
My variable cp is = 2 when I pass 1 and 2 values but I get a null pointer exception.

Could you please explain?

Expand|Select|Wrap|Line Numbers
  1.  
  2. for(int i=0; i<=resLeftL.size(); i++){
  3.   //System.out.println(resLeftL.size());      
  4.   for(int j=0; j<= resRightL.size(); j++)
  5.   {  
  6.     cP=resLeftL.get(i)*resRightL.get(j);
  7.     System.out.println("cp="+cP); // prints 2
  8.     elements.addElements(cP); 
  9.     //dereferecing null
  10.    }   
  11.  }
  12.  return elements;
  13.  }
Oct 30 '13 #1
9 2848
r035198x
13,262 MVP
Did you check if elements is null at that point?
Oct 31 '13 #2
oll3i
679 Contributor
Expand|Select|Wrap|Line Numbers
  1. elements.addElement(resLeftL.get(i)*resRightL.get(j));       
<--is null at that point
Oct 31 '13 #3
oll3i
679 Contributor
the error can be here
Expand|Select|Wrap|Line Numbers
  1.  IBagResult  elements = null;
and the BagResult is
Expand|Select|Wrap|Line Numbers
  1. public class BagResult implements IBagResult{
  2.  Collection collection = new HashSet();
  3.  @Override
  4.  public Collection getElements()
  5.  {return collection;} 
  6.  @Override
  7.  public void addElement(Integer i){
  8.  collection.add(i);
  9.  }
  10. }
Oct 31 '13 #4
Nepomuk
3,112 Recognized Expert Specialist
Do you have something like
Expand|Select|Wrap|Line Numbers
  1. elements = new BagResult();
anywhere? If not, add that before you try to access the elements object.
Oct 31 '13 #5
oll3i
679 Contributor
Expand|Select|Wrap|Line Numbers
  1. elements.addElement(resLeftL.get(i)*resRightL.get(j))
returns

thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at s4409_s4409_pr2.QExecUtils.cartesianProduct(QExecU tils.java:60)
at s4409_s4409_pr2.S4409_s4409_pr2.main(S4409_s4409_p r2.java:38)
Oct 31 '13 #6
oll3i
679 Contributor
and in main that is the line
Expand|Select|Wrap|Line Numbers
  1. IBagResult commaRes = QExecUtils.cartesianProduct(resLeft,resRight
Oct 31 '13 #7
Nepomuk
3,112 Recognized Expert Specialist
As the loop is dependant on the size of resLeftL, I'd guess that resRightL is smaller than the left list. To be more precise, according to this exception it has one element and you're trying to access the second element.
Oct 31 '13 #8
Frinavale
9,735 Recognized Expert Moderator Expert
The index problem is because of the loop conditions you have:
Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i<=resLeftL.size(); i++)
  2. {
  3.   for(int j=0; j<= resRightL.size(); j++)
  4.   {
  5.   }
  6. }
Arrays etc. in Java start at index 0. This means that if you have something that is 10 units long the valid indices fall between 0 and 9 but in your loop you are going to 10 because of the <= check. It should just be <.



-Frinny
Oct 31 '13 #9
Nepomuk
3,112 Recognized Expert Specialist
Good catch Frinny, I missed that one!
Oct 31 '13 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
4311
by: jennifer johnson | last post by:
Hello All, I appreciate anyone's assistance. I wanted to validate user customizable field values so I changed my JSP page so that the form action would post existing form - action=<%=...
1
7296
by: Mark | last post by:
Hi, I'm writing user controls with custom events. I have a parent custom event that exposes some abstract methods and some custom events. I have also created some new user controls that...
5
2831
by: Tony Cheng | last post by:
for (int i=0; i<_request.Form.Count; i++ ) { string key = _request.Form.GetKey(i); if ( !key.Equals("formCode") && !key.Equals("formLanguage") && !key.Equals("__VIEWSTATE") &&...
3
6793
by: chramprasad | last post by:
public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ...
1
13074
by: zahidkhan | last post by:
Hi All, Plz help me if you can..... I have a program something like this int main(int argc,char* argv) { try { int* p = NULL;
1
3480
by: MRamaLakshmi | last post by:
hi, I am developing an application using Java Applet which will be uploading files. Its throwing Null Pointer exception while detecting the proxy when we are trying to load the applet using Java6....
1
1082
by: bansalrichaacc | last post by:
In The Following Code I am Getting Null Pointer Exception try{ ClientBean clientInfo = clientManager.getClientList(); System.out.println("richa2"+clientInfo); if (clientInfo != null )...
11
7036
by: ajos | last post by:
hi friends, seems as if though this null pointers wont let me go peacefully home!!!! i have a jsp page where i do a get attribute like this---> <%String s24=(String) request.getAttribute("s24");...
5
2325
by: iamdennisthomas | last post by:
Hi Guys i was developing a action servlet which is actulally getting the data from a form and putting it in the db but while executing i am getting a null pointer exception /* * Generated by...
1
1828
by: Riteshti | last post by:
Im trying this java code to export data from database using sql query, But getting a null pointer exception. getting no clue to remove it . Im getting this exception at line no 26. try { ...
0
7046
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7088
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5342
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4485
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2997
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
183
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.