Martyn Martyn - 1 year ago 72
Java Question

Extending JComboBox, errors when building with addItem and String []

I've created my own

and using an
method to add the items. I can't understand why it's working from one
String []
(see below, commented out,"Bird", "Cat", etc) and not another (
which returns also
String []

Below is my custom

public class CategoryComboBox extends JComboBox<String> {

private static final long serialVersionUID = 1L;

private Transactions transactions;

public CategoryComboBox(Transactions transactions) {

public void update() {
// String[] categories = { "Bird", "Cat", "Dog", "Rabbit", "Pig" }; // works
String[] categories = transactions.getCategoriesArray(); // line 24, errors
for( String c : categories ) {

If it helps, here is my method of the transactions object's class:

public class Transactions {


public String [] getCategoriesArray() {
String[] categories = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
return categories;

When I click Run... in Eclipse I get the following errors in the console:

Exception in thread "main" java.lang.NullPointerException
at biz.martyn.budget.CategoryComboBox.update(
at biz.martyn.budget.CategoryComboBox.<init>(
at biz.martyn.budget.NewTransactionDialog.<init>(
at biz.martyn.budget.TransactionsToolbar.<init>(
at biz.martyn.budget.Budget.main(

Should there be anything different in either case?

Answer Source

Not sure, but I think you should add a line to initialize your transactions filed you the constructor. The NullPointerException thrown because you didn't initialize the field.

public CategoryComboBox(Transactions transactions) {
    this.transactions = transactions;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download