Hello,

I am working on a project and am in need of search function.

How my system works:

User enters details
Stored into a text file
writes a blank line at the end of the record
when reading in the file, a new blank line = new record

So for example, userID, forename, surname would look like this in the text file:

1
John
Jones

2
Gareth
Williams

I can read and write just fine, but I am trying to setup search capabilities for the userID's.

I was wondering what's the best way to approach this?

This is all I have so far, I think I am partially there but I believe my errors lie within the for statement:

 public void searchInvoice() {

        String idInputString  = JOptionPane.showInputDialog(null, "Please enter the Order ID you're searching for:");
        int idInput = Integer.parseInt(idInputString);

        for (int i = 0; i <= orderID.length; i++) {

            if(idInput == i) {
                txtInvoiceOrderID.setText(orderID[i]);
                txtInvoiceForename.setText(customerForename[i]);
                txtInvoiceSurname.setText(customerSurname[i]);
            } else {
                JOptionPane.showMessageDialog(null, "Order ID Not Found", "Error!", JOptionPane.WARNING_MESSAGE);
            }

        }
    }

Many thanks for your time.

my errors lie within the for statement:

Please explain what the errors are. Post the full text of any error messages.

OK, so I have an id that matches "2"

so when I enter 2 into the criteria this happens:

I get spammed with multiple error message boxes and then it crashes and I recieve:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at avp.Invoices.searchInvoice(Invoices.java:363)
    at avp.Invoices.cmdFindActionPerformed(Invoices.java:909)
    at avp.Invoices.access$300(Invoices.java:13)
    at avp.Invoices$4.actionPerformed(Invoices.java:619)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6504)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6269)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4860)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2713)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at avp.Invoices.searchInvoice(Invoices.java:363)

There is a variable on line 363 that has a null value when you tried to use it. Find the variable that has the null value and backtrack in the code to see why it does not have a valid non-null value.

the line on that is one of my radio buttons, it's value is stored as false or true, currently stored as false as it was not checked.

Why did the variable have a null value? That needs to be fixed.

I don't understand why it thinks it is null, I have 3 radio buttons.

The text file looks like this:
false
true
false

it loads the file perfectly into the text fields, but when I try to search for it using the invoice's ID number, it gives me that error.

why it thinks it is null,

Computers are good at finding holes in your logic.
Do you know what variable has the null value? Look at where it is defined and where it is given a value.
Make sure that there is only one definition for the variable with the name of the one with the null value.

The error is on line 363 which is a radio button:

if (basicWash[i].equals("true")) {
                    radioInvoiceBasic.setSelected(true);

                }else {
                    radioInvoiceBasic.setSelected(false);
                }

in the text file that is is reading from, the value is 'false'

I don't even know why it's throwing back an error as it shouldn't find anything as I think my for loop is wrong, there are no null values otherwise the file wouldn't be read properly.

I am trying to achieve:

orderID stored in text file as:

1
forename1
surname1

2
forename2
surname2

MessageBox(" Enter Order ID You want to search for")
User enters: "2"

Loop through array until orderID = 2.
Display the details corresponding to order ID 2
else
Error message, no ID found.

no null values otherwise the file wouldn't be read properly.

Not true. Only one variable unrelated to reading a file can cause the NPE.

Which line in the code you posted is the line with the NPE: 1, 2 or 5?

You need to find the variable that has a null value. Add a println statement before the statement that where the NPE occurs and print out the values of the variables on the line where the NPE occurs.

the NPE points to 1.

I did the println as you recommended, everything is printed out fine.

The issue is:

Text File:
1
forename1
surname1

2
forename2
surname2

Search for:

ID: 1

What EVER happens it println's the second (last) record.

I cannot figure out why it gives me a NPE as it prints out the correct value (false)

the NPE points to 1.

Print out the value of the variable on that line and the value of i

If I enter 2 into the input box and use this code:

System.out.println(basicWash[i]);
System.out.println(i);

the out put is:

false
0
false
1

Where is the NPE error message?

Full Output:

run:
false
0
false
1
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at avp.Invoices.searchInvoice(Invoices.java:363)
    at avp.Invoices.cmdFindActionPerformed(Invoices.java:940)
    at avp.Invoices.access$300(Invoices.java:13)
    at avp.Invoices$4.actionPerformed(Invoices.java:650)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6504)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6269)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4860)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2713)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

What code is at line 363 where the NPE occurs? You are not printing out the right variables because you would print out a null before the NPE occurs.

OK here is the code:

 public void searchInvoice() {

        String idInputString  = JOptionPane.showInputDialog(null, "Please enter the Order ID you're searching for:");
        int idInput = Integer.parseInt(idInputString);

        for (int i = 0; i <= orderID.length; i++) {

            if(idInput == i) {
                txtInvoiceOrderID.setText(orderID[i]);
                txtInvoiceForename.setText(customerForename[i]);
                txtInvoiceSurname.setText(customerSurname[i]);
                txtInvoiceDoB.setText(customerDoB[i]);
                txtInvoiceAddress1.setText(address1[i]);
                txtInvoiceAddress2.setText(address2[i]);
                txtInvoiceTown.setText(town[i]);
                txtInvoiceCounty.setText(county[i]);
                txtInvoicePost.setText(postCode[i]);
                txtInvoiceCarModel.setText(carModel[i]);
                txtInvoiceCarColour.setText(carColour[i]);
                txtInvoiceCarReg.setText(carReg[i]);
                txtInvoiceStartDate.setText(startDate[i]);
                txtInvoiceStartTime.setText(startTime[i]);
                txtInvoiceEndDate.setText(endDate[i]);
                txtInvoiceEndTime.setText(endTime[i]);
                txtInvoiceDaysParked.setText(days[i]);
                txtInvoiceSerial.setText(serialNum[i]);
                if (basicWash[i].equals("true")) {
                    radioInvoiceBasic.setSelected(true);

                }else {
                    radioInvoiceBasic.setSelected(false);
                }
                if (standardWash[i].equals("true")) {
                    radioInvoiceStandard.setSelected(true);

                }else {
                    radioInvoiceStandard.setSelected(false);
                }
                if (premiumWash[i].equals("true")) {
                    radioInvoicePremium.setSelected(true);

                }else {
                    radioInvoicePremium.setSelected(false);
                }
                txtInvoiceTotal.setText(total[i]);

LINE 363 =

 if (basicWash[i].equals("true")) {

My printout code is:

} else {
                JOptionPane.showMessageDialog(null, "Order ID Not Found", "Error!", JOptionPane.WARNING_MESSAGE);
                System.out.println(basicWash[i]);
                System.out.println(i);

            }

Is that OK?

You need to have the println statements immediately BEFORE line 363 so you see the values of the variables BEFORE the NPE happens.

System.out.println(orderID[i]);
                System.err.println(i);
                System.out.println(customerForename[i]);
                System.err.println(i);
                System.out.println(customerSurname[i]);
                System.err.println(i);
                System.out.println(customerDoB[i]);
                System.err.println(i);
                System.out.println(address1[i]);
                System.err.println(i);
                System.out.println(address2[i]);
                System.err.println(i);
                System.out.println(town[i]);
                System.err.println(i);
                System.out.println(county[i]);
                System.err.println(i);
                System.out.println(postCode[i]);
                System.err.println(i);
                System.out.println(carModel[i]);
                System.err.println(i);
                System.out.println(carColour[i]);
                System.err.println(i);
                System.out.println(carReg[i]);
                System.err.println(i);
                System.out.println(startDate[i]);
                System.err.println(i);
                System.out.println(startTime[i]);
                System.err.println(i);
                System.out.println(endDate[i]);
                System.err.println(i);
                System.out.println(endTime[i]);
                System.err.println(i);
                System.out.println(days[i]);
                System.err.println(i);
                System.out.println(serialNum[i]);
                System.err.println(i);
                System.out.println(basicWash[i]);
                System.err.println(i);

Produces:

1
0
John
0
Williams
0
11/04/1989
0
23, The Road
0
Avenue
0
England
0
UK
0
ABCDEFG
0
Mercedes
0
Silver
0
57AMDAW1
0
05/05/2012
0
9:00am
0
10/05/2012
0
9:00am
0
5
0
f3d79831-29e5-430f-a65f-da2c06d418e7
0
false
0
2
1
Andrew
1
Jones
1
19/01/1983
1
23, The View
1
Avenue
1
Wales
1
1
UK
1
GETYAWD
1
Peugot
1
Silver
1
57AMDAW1
1
03/05/2012
1
1
9:00am
12/05/2012
9:00am
1
5
f4912-12412uda-1421dada-532ndada
1
false
1
1
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at avp.Invoices.searchInvoice(Invoices.java:363)
    at avp.Invoices.cmdFindActionPerformed(Invoices.java:978)
    at avp.Invoices.access$300(Invoices.java:13)
    at avp.Invoices$4.actionPerformed(Invoices.java:688)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6504)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6269)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4860)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2713)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

There are no null values.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at avp.Invoices.searchInvoice(Invoices.java:363)

The line number in the error message is the same. That means you did NOT change the program by adding println statements immediately before line 363. If you had added lines before line 363 the line number would be incremented by 1 for every line you added.

When you print so many values, you need to add an id String with the value so you know what value was printed. For example:

System.err.println("i=" + i);
System.out.println("town=" +town[i]);
 public void searchInvoice() {

        String idInputString  = JOptionPane.showInputDialog(null, "Please enter the Order ID you're searching for:");
        int idInput = Integer.parseInt(idInputString);

        for (int i = 0; i <= orderID.length; i++) {

            if(idInput == i) {
                System.err.println("i=" + i);
                System.out.println("orderID= " + orderID[i]);
                txtInvoiceOrderID.setText(orderID[i]);

                System.err.println("i=" + i);
                System.out.println("customerForename= " + customerForename[i]);
                txtInvoiceForename.setText(customerForename[i]);

                System.err.println("i=" + i);
                System.out.println("customerSurname= " + customerSurname[i]);
                txtInvoiceSurname.setText(customerSurname[i]);

                System.err.println("i=" + i);
                System.out.println("customerDoB= " + customerDoB[i]);
                txtInvoiceDoB.setText(customerDoB[i]);

                System.err.println("i=" + i);
                System.out.println("address1= " + address1[i]);
                txtInvoiceAddress1.setText(address1[i]);

                System.err.println("i=" + i);
                System.out.println("address2= " + address2[i]);
                txtInvoiceAddress2.setText(address2[i]);

                System.err.println("i=" + i);
                System.out.println("town= " + town[i]);
                txtInvoiceTown.setText(town[i]);

                System.err.println("i=" + i);
                System.out.println("county= " + county[i]);
                txtInvoiceCounty.setText(county[i]);

                System.err.println("i=" + i);
                System.out.println("postCode= " + postCode[i]);
                txtInvoicePost.setText(postCode[i]);

                System.err.println("i=" + i);
                System.out.println("carModel= " + carModel[i]);
                txtInvoiceCarModel.setText(carModel[i]);

                System.err.println("i=" + i);
                System.out.println("carColour= " + carColour[i]);
                txtInvoiceCarColour.setText(carColour[i]);

                System.err.println("i=" + i);
                System.out.println("carReg= " + carReg[i]);
                txtInvoiceCarReg.setText(carReg[i]);

                System.err.println("i=" + i);
                System.out.println("startDate= " + startDate[i]);
                txtInvoiceStartDate.setText(startDate[i]);

                System.err.println("i=" + i);
                System.out.println("startTime= " + startTime[i]);
                txtInvoiceStartTime.setText(startTime[i]);

                System.err.println("i=" + i);
                System.out.println("endDate= " + endDate[i]);
                txtInvoiceEndDate.setText(endDate[i]);

                System.err.println("i=" + i);
                System.out.println("endTime= " + endTime[i]);
                txtInvoiceEndTime.setText(endTime[i]);

                System.err.println("i=" + i);
                System.out.println("days= " + days[i]);
                txtInvoiceDaysParked.setText(days[i]);

                System.err.println("i=" + i);
                System.out.println("serialNum= " + serialNum[i]);
                txtInvoiceSerial.setText(serialNum[i]);

                System.err.println("i=" + i);
                System.out.println("basicWash= " + basicWash[i]);
                if (basicWash[i].equals("true")) {
                    radioInvoiceBasic.setSelected(true);

                }else {
                    radioInvoiceBasic.setSelected(false);
                }

If I search 0 (which shouldnt exist I get:

i=0
orderID= 1
i=0
customerForename= John
i=0
customerSurname= Jones
i=0
customerDoB= 13/04/1989
i=0
address1= 23, The Road
i=0
address2= Avenue
i=0
town= England
i=0
county= UK
i=0
postCode= ABCDEF
i=0
carModel= Mercedes
i=0
carColour= Silver
i=0
carReg= 57AMDAW1
i=0
startDate= 05/05/2012
i=0
startTime= 9:00am
i=0
endDate= 10/05/2012
i=0
endTime= 9:00am
i=0
days= 5
i=0
serialNum= f3d79831-29e5-430f-a65f-da2c06d418e7
i=0
basicWash= false

search 1:

run:
i=1
orderID= 2
i=1
customerForename= Katie
i=1
customerSurname= Williams
i=1
customerDoB= 09/09/1994
i=1
address1= 23, The Street
i=1
address2= Road
i=1
town= Wales
i=1
county= UK
i=1
postCode= FEGHADA
i=1
carModel= Peugot
i=1
carColour= Yellow
i=1
carReg= AWODAWJF
i=1
startDate= 09/12/2012
i=1
startTime= 10:00am
i=1
endDate= 10/01/2013
i=1
endTime= 11:00am
i=1
days= 4
i=1
serialNum= f3d79831-29e5-430f-a65f-da2c06d418e7
i=1
basicWash= false

OK, so it's selected it based on arrays, not the actual number presented in the orderID field, how is this fixed?

Really appreciate your patience, thanks.

Why do you print the value of i so many times when it has not been changed?

Where is the NPE error message. The printed values ONLY have meaning if they show the variable with the null value. The other print outs don't help any.

You only need one println immediately before the line with the NPE. The others do not help.

Input:
0

Output:

i=0
basicWash= false

no NPE.

Input:
1

Output:
i=1
basicWash= false

No NPE.

Input:
2

Output:

i=2
basicWash= null
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at avp.Invoices.searchInvoice(Invoices.java:402)
    at avp.Invoices.cmdFindActionPerformed(Invoices.java:1023)
    at avp.Invoices.access$300(Invoices.java:13)
    at avp.Invoices$4.actionPerformed(Invoices.java:733)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6504)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6269)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4860)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2713)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Finally:

i=2
basicWash= null

The third element (index = 2) in the array has a null value. Why is that? Do you expect the array to have null elements? If so, you need to test if the element has a null value before using it.

This is the problem, I am not searching for array. I am searching for the ID number.

So when I enter
"2"

I want it to look for the record that has orderID = 2

not array element 2.

Thanks for your help so far!

You need to change your logic so the for loop does not go past the number of elements in the array.

How do I go about doing that? I've never done something like this before.

for (int i = 0; i < orderID.length; i++) {

for (int i = 0; i <= orderID.length; i++) {

Both don't work.

for (int i = 0; i <= orderID.length - 1; i++) {
or something similar? should throw an error on 0 but be fine elsewhere?

Input: 1
That still displays the data stored in array[1]

NOT the data that has 'Order ID: 1'

This is my full code:

public void searchInvoice() {

        String idInputString  = JOptionPane.showInputDialog(null, "Please enter the Order ID you're searching for:");
        int idInput = Integer.parseInt(idInputString);

       for (int i = 0; i <= orderID.length - 1; i++) {

            if(idInput == i) {
                System.err.println("i=" + i);

                txtInvoiceOrderID.setText(orderID[i]);


                txtInvoiceForename.setText(customerForename[i]);


                txtInvoiceSurname.setText(customerSurname[i]);


                txtInvoiceDoB.setText(customerDoB[i]);


                txtInvoiceAddress1.setText(address1[i]);


                txtInvoiceAddress2.setText(address2[i]);


                txtInvoiceTown.setText(town[i]);


                txtInvoiceCounty.setText(county[i]);


                txtInvoicePost.setText(postCode[i]);


                txtInvoiceCarModel.setText(carModel[i]);


                txtInvoiceCarColour.setText(carColour[i]);


                txtInvoiceCarReg.setText(carReg[i]);


                txtInvoiceStartDate.setText(startDate[i]);


                txtInvoiceStartTime.setText(startTime[i]);


                txtInvoiceEndDate.setText(endDate[i]);


                txtInvoiceEndTime.setText(endTime[i]);


                txtInvoiceDaysParked.setText(days[i]);


                txtInvoiceSerial.setText(serialNum[i]);


                System.out.println("basicWash= " + basicWash[i]);
                if (basicWash[i].equals("true")) {
                    radioInvoiceBasic.setSelected(true);

                }else {
                    radioInvoiceBasic.setSelected(false);
                }


                if (standardWash[i].equals("true")) {
                    radioInvoiceStandard.setSelected(true);

                }else {
                    radioInvoiceStandard.setSelected(false);
                }


                if (premiumWash[i].equals("true")) {
                    radioInvoicePremium.setSelected(true);

                }else {
                    radioInvoicePremium.setSelected(false);
                }


                txtInvoiceTotal.setText(total[i]);



            } else {
                JOptionPane.showMessageDialog(null, "Order ID Not Found", "Error!", JOptionPane.WARNING_MESSAGE);


            }

        }
    }

Where is the count of the valid elements in the array? What varible has the count?
Remember the index of the last element is the count - 1. It there are two elements, the max index would be 1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.