Help: Bank Statement Program, Objects within Object wrapper, null pointer

I am trying to create a class to create a list of banking transactions. The list should hold the date and time of the transaction, the type of transaction (either deposit or withdrawal) and the amount of the transaction.

So I came up with this code:

import java.util.*;

import java.text.*;

import java.util.LinkedList;

public class Transaction

{

class transactionTypeWrapper{

Date now;

StringBuffer transactionType = new StringBuffer(8);

int amount;

}

transactionTypeWrapper aTransaction;

public Transaction()

{

LinkedList transactions = new LinkedList();

}

public void addTransaction(String transactionType, int amount)

{

StringBuffer temp = new StringBuffer(transactionType);

aTransaction.transactionType.insert(0, "Deposit");

aTransaction.amount = amount;

System.out.println("Transaction Successful");

}

}

Whenever I try to execute the addTransaction method however, it throws up a NullPointerException. i think it has problems with my putting a StringBuffer inside the transactionTypeWrapper but I don't know any other way to do what I want to do without doing this?

[1228 byte] By [String_pseudoNyma] at [2007-11-26 18:21:20]
# 1

Paste in the stack trace for the NPE, and tell us which line it's complaining about.

When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

jverda at 2007-7-9 5:55:09 > top of Java-index,Java Essentials,New To Java...
# 2
I'm simply running the method in BlueJ, don't know what the stack trace in the NPE is.The error appears to be on this line:aTransaction.transactionType.insert(0, "Deposit");
String_pseudoNyma at 2007-7-9 5:55:09 > top of Java-index,Java Essentials,New To Java...
# 3
The stack trace is the list of methods and line numbers that shows what called what to give the exception. NPE is NullPointerException.aTransaction.transactionType.insert(0, "Deposit");Either aTransaction is null, or aTransaction.transactionType is.
jverda at 2007-7-9 5:55:09 > top of Java-index,Java Essentials,New To Java...
# 4
Yeah but I can't see why or how it is null? I've initialised it and now i'm trying to put the value in but it wont accept it?
String_pseudoNyma at 2007-7-9 5:55:10 > top of Java-index,Java Essentials,New To Java...
# 5

transactionTypeWrapper aTransaction;

That never gets initialized. So when you call this code in addTransaction():

aTransaction.transactionType.insert(0, "Deposit");

It's really

null.transactionType.insert(0, "Deposit");

Maybe instead of creating a LinkedList in the constructor that disappears as soon as the constructor finishes, you should actually initailize aTransaction there.

Message was edited by:

hunter9000

hunter9000a at 2007-7-9 5:55:10 > top of Java-index,Java Essentials,New To Java...
# 6

> Yeah but I can't see why or how it is null? I've

> initialised it and now i'm trying to put the value in

> but it wont accept it?

Use a debugger or print statements to see what's happening. Either you're not initializing it, or you are but then you're setting it back to null, or there's more than one of them and you don't have the one you think you do.

jverda at 2007-7-9 5:55:10 > top of Java-index,Java Essentials,New To Java...
# 7

Right, now I have initialised it in the constructor like so

transactionTypeWrapper aTransaction = new transactionTypeWrapper();

But it is throwing up a new error on this same line

aTransaction.transactionType.insert(1, "Deposit");

Saying "package aTransaction does not exist".

String_pseudoNyma at 2007-7-9 5:55:10 > top of Java-index,Java Essentials,New To Java...
# 8
I'd suggest fleshing out your TransactionTypeWrapper class before proceeding. Give it a constructor, some accessors, initialize it's members, etc. If you don't want to use a getter for transactionType, try making it public. I don't use inner classes much, so that might not help.
hunter9000a at 2007-7-9 5:55:10 > top of Java-index,Java Essentials,New To Java...