• Post Reply Bookmark Topic Watch Topic
  • New Topic

StackOverFlowException  RSS feed

 
kalthum munir
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my java code is having a StackOverFlowException

import java.util.ArrayList;




public class cust {


private int Cid;
private String Cname;
private String Csurname;
private double Cpayment;
private boolean hiredhouseConfirmation;
public cust(){}
hH<?> hhire =new hH<Object>();


public int getCid() {
return Cid;
}
public void setCid(int cid) {
Cid = cid;
}
public String getCname() {
return Cname;
}
public void setCname(String cname) {
Cname = cname;
}
public String getCsurname() {
return Csurname;
}
public void setCsurname(String csurname) {
Csurname = csurname;
}
public double getCpayment() {
return Cpayment;
}
public void setCpayment(double cpayment) {
Cpayment = cpayment;
}

public void setHiredHouse(boolean hiredHouse) {
}

public boolean isHiredhouseConfirmation() {
return hiredhouseConfirmation;
}
public void setHiredhouseConfirmation(boolean hiredhouseConfirmation) {
this.hiredhouseConfirmation = hiredhouseConfirmation;
}



public void runHire(ArrayList<h>HiredL){
if(HiredL.size()==0){
hiredhouseConfirmation=false;
}
else{
hiredhouseConfirmation=true;
}

}
}




import java.util.ArrayList;
import java.util.Scanner;




public class hH<House> {


ArrayList<h>HiredL = new ArrayList<h>();
private double HiringPayment;
cust cus=new cust();
h hHire = new h(0, null, null, 0, 0);
Scanner hirein=new Scanner(System.in);
private int id;
private String name;
private String surname;
private double price;
public hH(){


}

public double getHiringPayment() {
return HiringPayment;
}

public void setHiringPayment(double hiringPayment) {
HiringPayment = hiringPayment;
}
public void makePayment(){

}

public ArrayList<h> Hire(ArrayList<h>HiredL){
System.out.println("Hire a house");
System.out.println("Enter your information");
id=hirein.nextInt();
name=hirein.next();
surname=hirein.next();

System.out.println("house list");
System.out.println(hHire.toString());


System.out.println("Enter the HOUSE id you wish to hire: ");
id=hirein.nextInt();
System.out.println("Enter the HOUSE price ");
price=hirein.nextDouble();
HiredL.add(hHire);
return HiredL;

}

}



my compiler shows this error message:
Exception in thread "main" java.lang.StackOverflowError
at java.util.ArrayList.<init>(Unknown Source)
at hH.<init>(hH.java:10)
at cust.<init>(cust.java:15)
at hH.<init>(hH.java:12)
at cust.<init>(cust.java:15)
at hH.<init>(hH.java:12)
at cust.<init>(cust.java:15)
at hH.<init>(hH.java:12)
I dont't really understand what I am suppose to look out for please if I could get a little push to what exactly is wrong.

 
Stuie Clarky
Ranch Hand
Posts: 98
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, use the code tag button to make it easier to read.
Second, should this not be a reply to your previous thread 'arrays and boolean'?

Other than that you should think about the advice you were given in the previous thread
 
kalthum munir
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i'm new to this:


my compiler shows this error message:

Exception in thread "main" java.lang.StackOverflowError
at java.util.ArrayList.<init>(Unknown Source)
at hH.<init>(hH.java:10)
at cust.<init>(cust.java:15)
at hH.<init>(hH.java:12)
at cust.<init>(cust.java:15)
at hH.<init>(hH.java:12)
at cust.<init>(cust.java:15)
at hH.<init>(hH.java:12) ]
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Kalthun, when you post code then it must be enclosed within the code tags. I have just added the code tag to your post

And the Second file as

Now based on this code, I am wondering how can you get a StackOverFlowException if you do not have any main method in either of the class. Another point of concern is the following code line in your second file hH.java
It would be appreciated if you post the code for the main method too. Also please follow the proper class and method naming conventions for Java.. what you have coded is so dammn cryptic that only Men from Mars would be able to comprehend it.
 
kalthum munir
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The code is actually part of a House hiring system I separated it to check the hiring a house method(Hire in hH class). I just declared my main and called the method.


I Know it makes no sense because i'm still trying to figure how to organise my arrayList. I have an idea of what the exception above is but what would I have to do to fix it and what caused the error?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a note: 'h' and 'hH' are really bad class names. How is anybody every supposed to understand what the purpose of a class named 'h' is? Use clear, descriptive names for your classes.

The StackOverflowError (note, it's not StackOverFlowException) happens because class Cust has a member of type 'hH' which is initialized when you create a new Cust, and also class hH has a member of type Cust which is initialized when you create a new hH.

So what happens: you create a new Cust, which creates a new hH, which creates a new Cust, which creates a new hH, which creates a new Cust, which creates a new hH, which creates a new Cust, which creates a new hH, ... etc. until the stack overflows.

See lines 9 and 7 of your following two code fragments.



 
kalthum munir
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot i'll see into it sorry for the inconvenient class names
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!