null pointer exception, not going inside loop - java

The requirement is:If <bby:SaleTenderType> value is eCommerce , it reads the SalesOrderNum and calls the required function(appends other nodes to the root node). <bby:SaleTenderType> is not eCommerce, it should call other function.
The java code is as below:
//Added for giftCard
//error
Lineitems = retailChildren.item(j).getChildNodes();
for (int z1 = 0; z1 < Lineitems.getLength(); z1++) {
if (Lineitems.item(z1).getNodeName().equalsIgnoreCase("Return")) {
NodeList returnChild1 = Lineitems.item(z1).getChildNodes();
for (int z2 = 0; z2 < returnChild1.getLength(); z2++) {
if (returnChild1.item(z2).getNodeName().equalsIgnoreCase("Tender")) {
NodeList tenderChild = Lineitems.item(z2).getChildNodes(); //Null pointer exception
for (int z3 = 0; z3 < tenderChild.getLength(); z3++) {
if (tenderChild.item(z3).getNodeName().equalsIgnoreCase("bby:SaleTenderType")) {
System.out.println("Inside");
String SaleTenderType = tenderChild.item(z3).getFirstChild().getNodeValue();
if (SaleTenderType.equalsIgnoreCase("eCommerce")) {
Lineitems = retailChildren.item(j).getChildNodes();
for (int i2 = 0; i2 < Lineitems.getLength(); i2++) {
if (Lineitems.item(i2).getNodeName().equalsIgnoreCase("Return")) {
NodeList returnChild = Lineitems.item(i2).getChildNodes();
for (int k = 0; k < returnChild.getLength(); k++) {
if (returnChild.item(k).getNodeName().equalsIgnoreCase("c360:SalesOrder")) {
String POSLOGSalesOrderNo = returnChild.item(k).getFirstChild().getNodeValue();
SalesOrderNo = POSLOGSalesOrderNo.substring(2, 12);
String SalesOrderNum = POSLOGSalesOrderNo.substring(0, 2);
if (SalesOrderNum.equalsIgnoreCase("HD") || (SalesOrderNum.equalsIgnoreCase("SV")) || (SalesOrderNum.equalsIgnoreCase("CR")) || (SalesOrderNum.equalsIgnoreCase("CW"))) {
if (!prevSalesOrder.equals(SalesOrderNo)) {
IDocEl = outdoc.createElement("IDOC");
ORDERS05.appendChild(createIDocHeader_GC(POSLOGSalesOrderNo, outdoc, EMPST, IDocEl, StoreID, true, returnChild, TRANS));
prevSalesOrder = SalesOrderNo;
}
IDocEl.appendChild(createE1EDP01forReturnLineItem_GC(outdoc, returnChild, StoreID));
}
}
}
}
}
}
}
}
} else {
Lineitems = retailChildren.item(j).getChildNodes();
for (int i3 = 0; i3 < Lineitems.getLength(); i3++) {
if (Lineitems.item(i3).getNodeName().equalsIgnoreCase("Return")) {
NodeList returnChild = Lineitems.item(i3).getChildNodes();
for (int k = 0; k < returnChild.getLength(); k++) {
if (returnChild.item(k).getNodeName().equalsIgnoreCase("c360:SalesOrder")) {
String POSLOGSalesOrderNo = returnChild.item(k).getFirstChild().getNodeValue();
SalesOrderNo = POSLOGSalesOrderNo.substring(2, 12);
String SalesOrderNum = POSLOGSalesOrderNo.substring(0, 2);
if (SalesOrderNum.equalsIgnoreCase("HD") || (SalesOrderNum.equalsIgnoreCase("SV")) || (SalesOrderNum.equalsIgnoreCase("CR")) || (SalesOrderNum.equalsIgnoreCase("CW"))) {
if (!prevSalesOrder.equals(SalesOrderNo)) {
IDocEl = outdoc.createElement("IDOC");
ORDERS05.appendChild(createIDocHeader(POSLOGSalesOrderNo, outdoc, EMPST, IDocEl, StoreID, true, returnChild));
prevSalesOrder = SalesOrderNo;
}
IDocEl.appendChild(createE1EDP01forReturnLineItem(outdoc, returnChild, StoreID));
}
}
}
}
}
}
}
}
}
//end of gift card
Here I am reading root node <Return> then checking whether it contains <Tender> .. If <Tender> is present ( obviously <bby:SaleTenderType> will be present).. else if <Tender> is not present, then other function is done.
I am getting Null pointer exception in this line NodeList tenderChild = Lineitems.item(z2).getChildNodes();
Kindly provide your valuable inputs
Thanks in advance..

Related

how to sort ArrayList by id and add to ArrayList ArrayList<ArrayList<DataPost>>

I have mList2 with values. There are values with the same id. How can I get a List or ArrayList in which objects with the same id are grouped and add it to ArrayList>?
List<ProfileActivity.DataPost> mList2 = list;
List<List<ProfileActivity.DataPost>> output = new ArrayList<List<ProfileActivity.DataPost>>();
List<ProfileActivity.DataPost> itemsAlreadyGrouped = new ArrayList<ProfileActivity.DataPost>();
for (int i = 0; i < mList2.size(); i++) {
List<ProfileActivity.DataPost> groupList = new ArrayList<ProfileActivity.DataPost>();
boolean groupCandidateFound = false;
if (!itemsAlreadyGrouped.contains(mList2.get(i))) {
for (int j = 0; j < mList2.size(); j++) {
if (mList2.get(i).getIds_post().equals(mList2.get(j).getIds_post())) {
groupList.add(mList2.get(i));
groupCandidateFound = true;
}
}
if (groupCandidateFound) {
itemsAlreadyGrouped.add(mList2.get(i));
}
}
if (groupList.size() > 0) {
output.add(groupList);
}
}
//Let's test the logic
for (List<ProfileActivity.DataPost> group : output) {
System.out.println(group);
Toast.makeText(context, group.toString(),Toast.LENGTH_SHORT ).show();
}
DataPost
data class DataPost(var text:String? = null, var photo:String? = null,
var type:String = "",
var ids_post:String = "", var position:String? = null)
Make your ProfileActivity.DataPost class implements Comparable<ProfileActivity.DataPost> interface, the implement the compareTo(ProfileActivity.DataPost o) method
#Override
public void compareTo(ProfileActivity.DataPost o){
return getIds_post().compareTo(o.getIds_post());
}
Then just invoke Collections.sort(list)

Remove nulls from an array in java

I have a method what should delete values from an array...
public Application[] deleteApp(String id) {
int count = 0;
for (int i = 0; i < this.apps.length; i++) {
if (this.apps[i] != null && this.apps[i].getId().equals(id)) {
this.apps[i] = null;
if (this.apps[i] == null)
count++;
}
}
Application[] withoutNulls = new Application[this.apps.length - count];
int index = 0;
for (Application app : this.apps) {
if (app != null) {
withoutNulls[index] = app;
index++;
}
}
return withoutNulls;
}
But final result is smth like:
Application[] app = {app1, app2, null};
What's wrong? I'm counting nulls, creating new array[sizeOfArray - countOfNulls], and recording only not-null values :(
upd
I have a test of that.
#Test
public void deleteAppTest() {
Tracker tracker = new Tracker();
Application testing_1 = new Application();
Application testing_2 = new Application();
Application[] test = {testing_1};
tracker.addApp(testing_1);
tracker.addApp(testing_2);
tracker.deleteApp(testing_2.getId());
assertThat(tracker.showApps(), is(test));
But method does not pass the test.
Expected: is []
but: was [, null]
Try with Lambdas
public Application[] deleteApp(String id) {
List<Application> result = Arrays.asList(apps).stream().
filter(app -> app != null &&!id.equals(app.getId())).collect(Collectors.toList());
return result.toArray(new Application[result.size()]);
}
Here it is as #Erwin Bolwidt suggested
public Application[] deleteApp(String id) {
int count = 0;
for (int i = 0; i < this.apps.length; i++) {
if (this.apps[i] != null && this.apps[i].getId().equals(id)) {
this.apps[i] = null;
count++;
}
// Move this if out into an else if
else if (this.apps[i] == null){
count++;
}
}
Application[] withoutNulls = new Application[this.apps.length - count];
int index = 0;
for (Application app : this.apps) {
if (app != null) {
withoutNulls[index] = app;
index++;
}
}
return withoutNulls;
}

Passing array of objects in java and saving the array of objects in the called method

I wrote some code to return me an array of objects. How do I save those objects in the called method?
public Ticket[] getOpenTicket() {
int ticketcount = 0;
for (int i = 0; i < ticket.length; i++) {
if (ticket[i] != null && ticket[i].getResolvedBy() == null) {
ticketcount = ticketcount + 1;
// System.out.println("Ticket raised by : " +
// ticket[i].getTicketno());
}
}
Ticket[] opentickets = new Ticket[ticketcount];
for (int i = 0; i < ticket.length; i++) {
if (ticket[i].getResolvedBy() == null) {
opentickets[i] = ticket[i];
}
}
return opentickets;
}
This is the called function from where I am calling openticket:
TicketDaoMemImpl tdmi=new TicketDaoMemImpl();
Ticket [] obj1=tdmi.getOpenTicket();
Thanks
Shouldn't that look more like this:
public class CheckTicket {
public Ticket [] openTicket() {
return arrayOfTickets; // wherever that comes from
}
}
CheckTicket cc = new CheckTicket();
Ticket[] t1 = cc.openTicket();
In this line of code
Ticket[] opentickets = new Ticket[ticketcount];
for (int i = 0; i < ticket.length; i++) {
if (ticket[i].getResolvedBy() == null) {
can't ticket[i] be null? It seems like that is most likely causing your issue - you call a method on what may be a null reference.
You should change you loop to something like:
Ticket[] opentickets = new Ticket[ticketcount];
int ticketIndex = 0;
for (int i = 0; i < ticket.length; i++) {
if (ticket[i] != null && ticket[i].getResolvedBy() == null) {
opentickets[ticketIndex] = ticket[i];
ticketIndex++;
}
}

How to give variable of object-2 the same value as a variable in object-1?

Language:
Java.
Aim:
give variable name from object rooms[] (class: Room) the same value as variable nameGuest from object guest1 (class: Guest).
problem:
this doesn't work
void printRooms(){
for(int i = 0; i < rooms.length; i++){
rooms[i].roomNr = ""+(i+1);
for(int j = 0; j < 2; j++){
a = guest1.getName();
rooms[i].name = a;
}//end for
}//end for
}//end void
Reviewing:
I've tried rooms[i].name = guest1.nameGuest. That did not work. Making a method getName to return nameGuest didn't work either. Same goes for making a pit-stop variable a to store the value.
I'm new to inter-class coding and am currently trying to read more about how to do it on https://docs.oracle.com/javase/tutorial/java/javaOO/ and http://www.tutorialspoint.com/java/java_object_classes.htm But it's not working out.
Question:
How do you give object2.variable value to object1.variable
In this case, hotel must have a method printRooms that gives room[i] a number and the name(s) of the guest(s).
Here's my full code:
public class Hotel2 {
Guest[] guest;
Room[] rooms;
String a;
void execute() {
rooms = new Room[10];
Guest guest1 = new Guest("Alain");
Guest guest2 = new Guest("Jantje");
for (int i = 0; i < 9; i++) {
rooms[i] = new Room(guest1);
}
//2 guests in 1 room
rooms[9] = new Room(new Guest[]{guest1, guest2});
System.out.println(Arrays.toString(rooms));
printRooms();
}
void printRooms() {
for (int i = 0; i < rooms.length; i++) {
rooms[i].roomNr = "" + (i + 1);
for (int j = 0; j < 2; j++) {
a = guest1.getName();
rooms[i].name = a;
}//end for
}//end for
}//end void
public static void main(String[] args) {
(new Hotel2()).execute();
}
}
class Room {
Guest[] guests;
String roomNr;
String name;
//1 guest constructor
Room(Guest g) {
guests = new Guest[1];
guests[0] = g;
}
//several guests constructor
Room(Guest[] g) {
guests = g;
}
}
class Guest {
String nameGuest;
Guest(String n) {
nameGuest = n;
System.out.println(nameGuest);
}
String getName() {
return nameGuest;
}
}
It's really hard to understand what you're asking.
Your problem seems to be with this code:
void printRooms() {
for (int i = 0; i < rooms.length; i++) {
rooms[i].roomNr = "" + (i + 1);
for (int j = 0; j < 2; j++) {
a = guest1.getName();
// ^^^^^^ Compiler error: cannot resolve symbol "guest1"
rooms[i].name = a;
}
}
}
Notice the comment ^^^^^ I put in there.
The code doesn't compile,
because the variable guest1 is undefined.
It's not defined inside the method,
and it's not defined as a field of the class.
If your intention is to overwrite each room's name using the first guest's name, this would be it:
void printRooms() {
for (int i = 0; i < rooms.length; i++) {
rooms[i].roomNr = "" + (i + 1);
rooms[i].name = room.guests[0].getName();
}
}
But this doesn't make sense for a method called printRooms,
as it prints nothing.
But I hope that based on this you'll figure out how to do what you really want to do.

Java - NullPointerException in a method

I'm writing a program that could manage universitary students with courses and subjects. The problem of the class I'm showing you is that when the method
public CorsoStudi(String unNomeCorso, ArrayList unElencoMaterie, int unIdCorso)
is called it throws a NullPointerException at line elencoEsamiDati.add(q);. So, probably the problem is with this line and the line after this:
EsameDato q = new EsameDato(unElencoMaterie.get(contatore), 0);
Eclipse doesn't advice error on writing code.
Creatore.java
import java.util.ArrayList;
import java.util.Scanner;
public class Creatore {
int counterStudenti = 0;
int counterMaterie = 0;
int counterCorsi = 0;
//int counterEsami = 0;
ArrayList<Studente> listaStudenti = new ArrayList<Studente>();
ArrayList<Materia> listaMaterie = new ArrayList<Materia>();
ArrayList<CorsoStudi> listaCorsi = new ArrayList<CorsoStudi>();
//ArrayList<EsameDato> listaEsami = new ArrayList<EsameDato>();
public void iscriviStudente(String nomeStudente, String cognomeStudente, CorsoStudi corsoStudente){
listaStudenti.add(new Studente(nomeStudente, cognomeStudente, counterStudenti, corsoStudente));
counterStudenti++;
}
public void reimmatricolaStudente(int unaMatricola, int unIdCorso){
int c = 0;
CorsoStudi unCorso = null;
for(c = 0; c < listaCorsi.size(); c++){
if(listaCorsi.get(c).getIdCorso() == unIdCorso)
unCorso = listaCorsi.get(c);
}
int contatore = 0;
for(contatore = 0; contatore < listaStudenti.size(); contatore++)
if(listaStudenti.get(contatore).getMatricola() == unaMatricola){
iscriviStudente(listaStudenti.get(contatore).getNome(), listaStudenti.get(contatore).getCognome(), unCorso);
rimuoviStudente(unaMatricola);
};
}
public void rimuoviStudente(int matricola){
int contatore;
for(contatore = 0; contatore < listaStudenti.size(); contatore++){
if(listaStudenti.get(contatore).getMatricola() == matricola)
listaStudenti.remove(contatore);
}
}
public Materia creaMateria(String nomeMateria, int crediti){
listaMaterie.add(new Materia( nomeMateria, crediti, counterMaterie));
counterMaterie++;
return listaMaterie.get(counterMaterie - 1);
}
public void creaCorsoStudi(String nomeCorso, ArrayList<Materia> materieCorso){
CorsoStudi q = new CorsoStudi( nomeCorso, materieCorso, counterCorsi);
listaCorsi.add(q);
counterCorsi++;
}
public ArrayList<Studente> cercaStudente(int opzione, String pattern){
int contatore = 0;
ArrayList<Studente> listaRicercati = new ArrayList<Studente>();
//opzione 1 = ricerca per nome
if(opzione == 1)
for(contatore = 0; contatore < listaStudenti.size(); contatore++){
if(listaStudenti.get(contatore).getNome().equalsIgnoreCase(pattern))
listaRicercati.add(listaStudenti.get(contatore));
};
//opzione 2 = ricerca per cognome
if(opzione == 2)
for(contatore = 0; contatore < listaStudenti.size(); contatore++){
if(listaStudenti.get(contatore).getCognome().equalsIgnoreCase(pattern))
listaRicercati.add(listaStudenti.get(contatore));
};
//opzione 3 = ricerca per matricola
if(opzione == 3)
for(contatore = 0; contatore < listaStudenti.size(); contatore++){
if(listaStudenti.get(contatore).getMatricola() == Integer.parseInt(pattern))
listaRicercati.add(listaStudenti.get(contatore));
};
//opzione 4 = ricerca per corsoStudi
if(opzione == 4)
for(contatore = 0; contatore < listaStudenti.size(); contatore++){
if(listaStudenti.get(contatore).getCorsoStudi().getIdCorso() == Integer.parseInt(pattern))
listaRicercati.add(listaStudenti.get(contatore));
};
return listaRicercati;
}
public Materia materiaDaId(int id){
int c = 0;
Materia materiaDaRitornare = null;
for(c = 0; c < listaMaterie.size(); c++){
if(listaMaterie.get(c).getIdMateria() == id)
materiaDaRitornare = listaMaterie.get(c);
}
return materiaDaRitornare;
}
}
CorsoStudi.java
import java.util.ArrayList;
import java.util.Scanner;
public class CorsoStudi {
private String nomeCorso;
private int idCorso;
private ArrayList<Materia> elencoMaterie;
private ArrayList<EsameDato> elencoEsamiDati;
public CorsoStudi(String unNomeCorso, ArrayList<Materia> unElencoMaterie, int unIdCorso){
nomeCorso = unNomeCorso;
elencoMaterie = unElencoMaterie;
idCorso = unIdCorso;
int contatore = 0;
//EsameDato q = null;
for(contatore = 0; contatore < unElencoMaterie.size(); contatore++){
EsameDato q = new EsameDato(unElencoMaterie.get(contatore), 0);
elencoEsamiDati.add(q);
};
}
public String getNomeCorso(){
return nomeCorso;
}
public int getIdCorso(){
return idCorso;
}
public ArrayList<Materia> getElencoMaterie(){
return elencoMaterie;
}
public ArrayList<EsameDato> getElencoEsamiDati(){
return elencoEsamiDati;
}
public String toString(){
String s = "";
s = s + "Ecco le materie di questo Corso di Studi:\n";
int c = 0;
for(c= 0; c < elencoMaterie.size(); c++){
s = s + elencoMaterie.get(c).getIdMateria() + " ";
s = s + elencoMaterie.get(c).getNomeMateria() + " (";
s = s + elencoMaterie.get(c).getCrediti() + " crediti)\n";
}
return s;
}
}
You have not initialized the field elencoEsamiDati therefore the actual value is null.
Before adding elements to an ArrayList you need to create it:
private ArrayList<EsameDato> elencoEsamiDati = new ArrayList<EsameDato>();
You may also need to initialize other fields as well.
BTW it is not a good idea to use your own language when programming, use English instead.

Categories