Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vector Null Exception

Status
Not open for further replies.

timmbo

Programmer
Feb 22, 2001
167
US
Hi All,

I have a method called to format data to a vector a certain way. Everything goes fine adding my format to a string myResults in j==3 if but when the code attempts to load the vector, acctResults.addElement(myResults);, I get a null pointer exception.

This is the call to the method:
Code:
...
 formatedResults = getACHFormatedAccouts(results);
...

Here is the called method:
Code:
public Vector getACHFormatedAccouts(Vector _unFormattedAccts) throws ISCException {	
  Vector acctResults = null;
		
  String myName = null;
  String myID = null;
  String myAchNbr = null;
  String myResults = null;
		
  int j = 0;
		
  for(int i=0; i < _unFormattedAccts.size(); i++) {
    j = j + 1;
			
    if (j == 1) {
      myName = (String)_unFormattedAccts.elementAt(i);		
    }
    if (j == 2) {
      myID = (String)_unFormattedAccts.elementAt(i);
    }
    if (j == 3) {
      myAchNbr = (String)_unFormattedAccts.elementAt(i);
      myResults = myName.trim() + " [" + myID.trim() + "] [" + myAchNbr.trim() + "]";
      System.out.println("******myResults is ... " + myResults);
      acctResults.addElement(myResults);
      System.out.println("*****After results.addElement...");
      j = 0;
    }
  }
  return acctResults;
}

Any suggestions/comments would be most appreciated.

TIA,
Tim
 
I should have mentioned that variable myResults is not null.
[5/2/05 14:40:17:745 EDT] 3cbafc2d SystemOut O ******myResults is ... ABC Co [ABC] [9876543210]
 
Hi,

Where are you initializing the Vector acctResults in getACHFormatedAccouts(Vector _unFormattedAccts);

Vector acctResults = null;

acctResults = new Vector();

Cheers
Venu
 
It's in public Vector getACHFormatedAccouts(Vector _unFormattedAccts) throws ISCException {
Vector acctResults = null;
 
That did Venur. Thanks for the help...
 
Hi,

In your code you are not creating a vector object you have initilized it to null. And your are trying to add an element to a null vector object which will trow a null pointer excpetion.

Code:
public Vector getACHFormatedAccouts(Vector _unFormattedAccts) throws ISCException {    
  Vector acctResults = null;
  // add this line of code
  [COLOR=red]acctResults = new Vector()[/color red];      
  String myName = null;
  String myID = null;
  String myAchNbr = null;
  String myResults = null;
        
  int j = 0;
        
  for(int i=0; i < _unFormattedAccts.size(); i++) {
    j = j + 1;
            
    if (j == 1) {
      myName = (String)_unFormattedAccts.elementAt(i);        
    }
    if (j == 2) {
      myID = (String)_unFormattedAccts.elementAt(i);
    }
    if (j == 3) {
      myAchNbr = (String)_unFormattedAccts.elementAt(i);
      myResults = myName.trim() + " [" + myID.trim() + "] [" + myAchNbr.trim() + "]";
      System.out.println("******myResults is ... " + myResults);
      acctResults.addElement(myResults);
      System.out.println("*****After results.addElement...");
      j = 0;
    }
  }
  return acctResults;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top