that's stored in:
numberOfOrdersInArray
System.out.println(numberOfOrdersInArray)
Outputs:
44
(22 pieces of information, 2 records)
that's stored in:
numberOfOrdersInArray
System.out.println(numberOfOrdersInArray)
Outputs:
44
(22 pieces of information, 2 records)
Then why is the element at i=2 null? That would say there are only 2 items in the array.
There is nothing at i=2, if it gets to i=2 NPE is given every time.
That is the problem. Why is that element null? Why does the for loop index (i) go past the number of elements in the array and try to index the empty slot?
Why did you say there are 44 elements in the array if there are only 2????
System.out.println(numberOfOrdersInArray)
Outputs:
44
I only have two records in my text file, element 2 shouldn't exist. I have no idea why it attempts to surpass it.
How do I tell Java to search for orderID?
How is the records stored so you can look at each record's orderID? Is there a class that holds a record's data with a method that returns the orderID so you can compare it to the value being searched for?
That's it for tonight. Back tomorrow.
This is how my system works:
Data is stored into a text file:
orderID
Forename
Surname
Text File:
1
John
Jones
2
Katie
Williams
each blank line is read in as a new record.
The text file is then read into an array.
The array is used to loop through the records, reading them into the text fields.
Now, the file is read into the array, distributed into the invoice text fields and is displayed correctly.
I can see the text fields displaying the correct data in my GUI, and using my next and previous buttons can traverse the array. Displaying the next record in the array.
Now, I want to implement a search function.
User Clicks 'Search'
Message Input Box Appears
User Enters the ORDER ID that they require to view.
Array is looped through
if the the entered number is = an orderID , the data from that element is displayed
else, error message.
Am I being clear? This is very new to me and don't know how to explain it properly.
What is in the array? How do you determine if an element in the array is the one you want when you do a search on the array?
How many arrays does the code have? What is the relationship between one array and another?
If the arrays are different sizes, you can not use the same index in all of them.
I don't know, that's the problem. I want to search for what the user enters. So if they enter 2, searches the array for "2" (if possible, only the orderID part).
One array with 22 elements in it.
Sorry if I am being vague, as I said I don't know much about this!
What about the array with 2 elements? Why are you looking in that array at empty elements ?
What is in the array that you want to search?
static String[] orderID = new String[10000], customerForename = new String[10000], customerSurname = new String[10000],
customerDoB = new String[10000], address1 = new String[10000], address2 = new String[10000], town = new String[10000],
county = new String[10000], postCode = new String[10000], carModel = new String[10000], carColour = new String[10000],
carReg = new String[10000], startDate = new String[10000], startTime = new String[10000], endDate = new String[10000],
endTime = new String[10000], days = new String[10000], serialNum = new String[10000], basicWash = new String[10000],
standardWash = new String[10000], premiumWash = new String[10000], total = new String[10000];
Array.
I currently have two records array[0] and array[1]
When I search for "ORDER ID: 2" I want the code to loop through array0, search for the orderID (first part of the array), if it is not there, check array1, if it is not there error message and cancel, else if is there, take the current loop number, and use it to display the OTHER information that corresponds with that ID.
I.E:
Search for ID 1 (ID 1 NOT ARRAY ELEMENT 1, the orderID's available are 1 and 2)
Loop through the data in array0
orderID 1 is found
GUI displays:
1
John
Jones
Would this be a lot easier if I was just to start my orderID's from 0 and auto-increment them +1 each time an order is placed?
You have giant arrays of 10,000 elements each, of which only the first 2 elements are populated and the remainder are null. When you loop you loop through the whole array, so you are guaranteed to hit nulls sooner or later.
You need a variable that tells you how many actual records there are, and use that as the upper limit for your loops, eg
for (int i = 0; i < numberOfRecords, i++) {
if (orderID[i].equals(searchID)) // arrays[i] contain the record you were looking for
}
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 < numberOfRecords; i++) {
if (orderID[i].equals(idInputString)) {
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]);
}
}
System.out.println(numberOfRecords);
}
numRecords always = 0, how do I tell it to loop the array?
Check the code to see why numRecords (or numberOfRecords???) does not have the number of records in the arrays.
So I need something along the lines of:
int numberOfRecords = 0;
for (int i = 0; i < theArray.length; i ++)
if (theArray[i] != null)
numberOfRecords ++;
How do I access my array using .length?
Not that way.
You should count the number of elements as you are adding data to the arrays.
How do you keep track of where the next empty slot in an array is when you are adding data to the arrays?
That's stored in a seperate class.
I have:
orders form
invoice form
The orders are created on the orders form
But I am using the data retrieved from the orders.txt to display in the invoice form.
That's stored in a seperate class.
The number of records in the arrays should be stored in the same place the arrays are stored. You need that number to be able to access the arrays's contents.
OK sorted it.
System.out.println(numberOfOrdersInArray);
Now returns:
44
(22 items of information, 2 records)
Where do I go from here?
Thanks a lot for your patience, I am incredibly frustrated as my system is 30% completed, but if I fix this searching and displaying issue I can finish the system in under an hour and finally be done with it!
You need to resolve why the one array (where you get the NPE) only has 2 elements in it instead of 44.
Or perhaps the array has empty slots with the slot at index=2 being empty and others past there having values.
I am completely back to square one now.
The code that was giving the NPE is completely unfunctional.
What I have so far:
public void searchInvoice() {
String idInputString = JOptionPane.showInputDialog(null, "Please enter the Order ID you're searching for:");
for (int i = 0; i < numberOfOrdersInArray; i++) {
if (orderID[i].equals(idInputString)) {
Please explain what the problem is.
Store Data into Text Files:
1
John
Jones
2
Katie
Williams
orderID
forename
Surname
The text files are read into the array:
static String[] invoiceID = new String[10000], orderID = new String[10000], customerForename = new String[10000], customerSurname = new String[10000],
customerDoB = new String[10000], address1 = new String[10000], address2 = new String[10000], town = new String[10000],
county = new String[10000], postCode = new String[10000], carModel = new String[10000], carColour = new String[10000],
carReg = new String[10000], startDate = new String[10000], startTime = new String[10000], endDate = new String[10000],
endTime = new String[10000], days = new String[10000], serialNum = new String[10000], basicWash = new String[10000],
standardWash = new String[10000], premiumWash = new String[10000], total = new String[10000];
So now I have two records in my array:
1
John
Jones
2
Katie
Williams
(of course I am shortening, there are 22 total pieces of information per record)
Now -
I want to be able to search the array for the ORDER ID (first piece of information).
So, I am looking for Katie Williams' order..
Messagebox for Input: 2
(to search for order ID 2 | orderID's and array[0] array[1] etc etc are completely seperate, array[0] has the orderID of 1, array[1] has orderID of 2 etc.)
I then need it to loop through each element in the array until it matches orderID to 2
otherwise, produce an error message.
numberOfOrdersInArray is counted through a read method elsewhere and is stored.
I do not have any errors as now I have no code, because I am at a complete loss as to what I need to do.
Did I explain it well enough?
Does the equals() method on line 7 return true? That would mean you have found a match.
Try adding a println just before the if statement with the equals() method and print out the value of i and the value of the orderID[i] array element so you can see what the computer sees as the loop executes.
CODE USED:
for (int i = 0; i <= numberOfOrdersInArray; i++) {
System.out.println("i = " + i +" " + " orderID[i] = " + orderID[i]);
System.out.println("ID INPUT STRING: " + (idInputString));
System.out.println("orderID[i].equals(idInputString): " + orderID[i].equals(idInputString));
if (orderID[i].equals(idInputString)) {
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);
}else {
radioInvoiceStandard.setSelected(false);
}
if (premiumWash[i].equals("true")) {
radioInvoicePremium.setSelected(true);
}else {
radioInvoicePremium.setSelected(false);
}
txtInvoiceTotal.setText(total[i]);
}
System.out.println(numberOfOrdersInArray);
}
}
Input Used:
1
Output:
run:
i = 0 orderID[i] = 1
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
ID INPUT STRING: 1
orderID[i].equals(idInputString): false
44
i = 1 orderID[i] = 2
ID INPUT STRING: 1
orderID[i].equals(idInputString): false
44
i = 2 orderID[i] = null
ID INPUT STRING: 1
at avp.Invoices.searchInvoice(Invoices.java:345)
at avp.Invoices.cmdFindActionPerformed(Invoices.java:915)
at avp.Invoices.access$300(Invoices.java:13)
at avp.Invoices$4.actionPerformed(Invoices.java:625)
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)
Back to the NPE???? You know how to debug that because you did it yesterday.
Find the variable that is null and then look in the code to see why it is null.
System.out.println(numberOfOrdersInArray);
That println is missing an ID string. so all you see printed is a number without a label. Add an id String to it like you have for all the other things you are printing.
> OK sorted it.
> `System.out.println(numberOfOrdersInArray); `
> Now returns:
> ` 44`
> (22 items of information, 2 records)
Each array contains 2 items, so you must only loop up to index 1 in each array - the number you need is the number of records, NOT the number of items.
I am completely out of my depth here, really don't have a clue what's happening, oh well I guess that's what happens when you're taught how to println and expected to produce a whole system.
Sorry for wasting your time, I'm so sick of this I'll just take the huge mark cut instead.
Thank you.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.