Question
Asked 7 January 2021

[JAVA]How to fix exception in thread error when using array of objects?

I am doing atm gui for my project in java. In my opinion using array of objects is the best option to store customer data. When i try to access it in an ActionListener method, i get this error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException. This happens when i hit the register button. I tried to delete the array and use a normal object in the ActionListener and it worked smoothly but it is not useful to store multiple customer data. Is there another way to store these or to fix this error? I uploaded the source file so you can check the code. Thanks in advance.

All Answers (3)

Hi,
NullPointerExceptions are usually easy to diagnose. You need to take a look to the full stacktrace and see in which code line the exception is rised. For example, if you have a trace like this:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at listeners.Listeners.actionPerformed(Listeners.java:52) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) ...
then the NullPointerException occurs in line 52 of the Listeners class.
So my recommendation is: locate the code line in your class where the exception occurs, and check if the accessed variable has been initialized.
I hope this helps,
Regards
Abbas Cheddad
University of Tartu
Check with the above recommendation. Such error is often caused by trying to access a non-existing object.
Although your attached code is not complete, you could consider checking the array of objects which you constructed.
musteri musteriler[] = new musteri[40];
personel kadro[] = new personel[20];
In here you have just pre-allocated the array space. However, inside the array cells, no object has been created yet!
So, you need to initialize that with the empty constructor or a constructor with params in a for loop:
for (int i = 0; i < musteriler.length; i++) {
musteriler[i] = new musteri();
//Calling the empty constructor to initialize the instance variables -if any-
}
2 Recommendations
Jaafar Idrais
University Ibn Zohr - Agadir
Selam kardeş Muhammmed Ali Işık
You can use an ArrayList<YourType> and create your list and use the Add method everytime you want. Visibility of the list object is important especially when working with threads
Wolfgang Konle
Airbus Defence and Space, Friedrichshafen, Germany
Most likely you are generating an independent thread for each object in your array. If you ensure that the operations of every thread are completed before you are accessing the results, your thread error will vanish. (This problem is not Java specific)

Similar questions and discussions

Related Publications

Chapter
One of the most important functions in software is information organizing and storage, with the goal of using it and sharing it. Information is written on paper and stored in organized cabinets in real life where it can be retrieved from. Software applications do something similar. Information is written in files, files are organized in directories...
Chapter
We covered I/O fundamentals in the last chapter, where you learned how to read and write from console and how to use streams to read and write to files. In this chapter, you will learn how to work with file systems—for example, how to save a file/directory; create a file/directory; navigate directories; copy, move, or delete a file/directory; and s...
Article
In most computer systems, objects and resources need to be named. Files in a filesystem, hosts in a network, users on a host or objects in a object-oriented program are usually referred to by human-readable logical names. A naming service is a mechanism that is used for associating names with objects and obtaining handles to objects given their nam...
Got a technical question?
Get high-quality answers from experts.