5 Replies - 4219 Views - Last Post: 11 March 2012 - 01:18 AM Rate Topic: -----

#1 2patrickMurphy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 18-February 12

java.lang.StackOverflowError null

Posted 10 March 2012 - 10:57 PM

So, i would normally describe whats going on here. Show some code, then as i know from expierence you guys want to see the actual error aswell. Well the thing is this little project i started today isnt really meant to be used by anyone other then me so its most likely not good at all. Its a

Also my second project ever so im a beginner. Anyway i have lots of method calls going on to the point it creates a error. All i want to know is, how do i go about debugging what the exact cause of this error? Print screens? Debugger? etc etc btw the error message is VERY VERY long so i figured i got an infinite loop in my method calls and constructors being created by other classes.

What my program is? Well, I was taking a survey and only got like 22 responses so i decided to computer generate the rest using probability based on real-life statistics. And my program will update the statistics and probabilities as it goes based off a semi-random selection. It will also have a split probability for gender of the tester and age.

If you want to see code, i can copy paste it. But i warn you, its not pretty.

Is This A Good Question/Topic? 0
  • +

Replies To: java.lang.StackOverflowError null

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 204
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: java.lang.StackOverflowError null

Posted 10 March 2012 - 11:06 PM

stack overflow usually points to an infinite or near infinite loop. Without the code it will be hard to help diagnose the real problem.


On a side note, what kind of survey is it? How do you know that a random selection is representative of the population? Often the information gained on a survey gives as much insight into the survey's actual question as it does the people who are willing to take said survey.
Was This Post Helpful? 0
  • +
  • -

#3 2patrickMurphy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 18-February 12

Re: java.lang.StackOverflowError null

Posted 10 March 2012 - 11:17 PM

ok... Ill scrap the code on here then ill explain the survey and why it doesnt really matter about the actual results.

Tester class: Main interface of program basically
import java.util.Random;
import java.util.ArrayList;
/**
 * Write a description of class Tester here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tester
{
    private boolean male;
    private Random randomGenerator;
    private Probability probability;
    private Statistics stats;
    private ArrayList<Double> probabilities;
    private ArrayList<Answer> answers;
    private int age;
    
    public Tester()
    {
        randomGenerator = new Random();
        probability = new Probability();
        stats = new Statistics();
        double chance = randomGenerator.nextDouble();
        double gender = probability.getProbabilityOfMale();
        if(chance > gender)
        {
            male = false;
            stats.incrementFemale();
        }else{
            male = true;
            stats.incrementMale();
        }
        age = randomGenerator.nextInt(49)+16;
        int range = stats.getNumberOfQuestion();
        for(int i=0;i<range;i++)
        {
           probabilities = probability.probOfAnswer(i);
           answers = probability.getAnswers(i);
           chance = randomGenerator.nextDouble();
           int size = probabilities.size();
           for(int j=0;j<size;j++)
           {
             double qTemp = chance - probabilities.get(j);
             if(qTemp <= 0.0)
             {
                 Answer aTemp = answers.get(j);
                 aTemp.incrementCounter();
                 answers.set(j,aTemp);
              }
           }
        }
    }
}
   


Probability class: the name lol

import java.util.ArrayList;
/**
 * Write a description of class Probability here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Probability
{
    protected double probabilityOfMale;
    protected ArrayList<Double> probOfAnswer;
    private ArrayList<Answer> temp;
    private Survey survey;
    private Answer aTemp;
    private double sum;
    protected Statistics stats;
    public Probability()
    {
        stats = new Statistics();
        probOfAnswer = new ArrayList<Double>(0);
    }

    public ArrayList<Double> probOfAnswer(int index)
    {               
        temp = stats.getAnswers(index);
        int size = temp.size();
        for(int i=0;i<size;i++)
        {
            aTemp = temp.get(i);
            for(int j=0;j<size;j++)
            {
                Answer aTemp = temp.get(j);
                sum += (double)aTemp.getCounter(); 
            }
            double number = (double)aTemp.getCounter();
            probOfAnswer.add(number/sum);
            sum = 0;

        }
        return probOfAnswer;
    }
    
    public ArrayList<Answer> getAnswers(int index)
    {
        temp = stats.getAnswers(index);
        return temp;
    }

    public ArrayList<Double> getProbofAnswer()
    {
        return probOfAnswer;
    }

    public void probabilityOfMale()
    {        
        double male = (double)stats.getMale();
        double female = (double)stats.getFemale();
        probabilityOfMale = male / (male + female);
    }

    public double getProbabilityOfMale()
    {
        return probabilityOfMale;
    }
}



Statistics class: Hard-wiring my survey information questions and current responses.
    private Survey survey;
    private ArrayList<Answer> temp;
    private int male,female,numberOfQuestion;

    public Statistics()
    {
        int i = 0;
        numberOfQuestion = 0;
        male = 18;
        female = 3;
        survey = new Survey("Essential Study Skills");
        survey.addQuestion("A math student is most likely to do while studying for an exam?");
        while(i<2)
        {
            survey.addAnswer("study",0);
            i++;
        }
        i=0;
        while(i<2)
        {
            survey.addAnswer("math",0);
            i++;
        }
        i=0;
        survey.addAnswer("video games",0);
        survey.addAnswer("calculator",0);
        while(i<2)
        {
            survey.addAnswer("formula sheet",0);
            i++;
        }
        i=0;
        survey.addAnswer("watch tv",0);
        survey.addAnswer("erase things",0);
        while(i<4)
        {
            survey.addAnswer("example/regular problems",0);
            i++;
        }
        i =0;
        while (i<2)
        {
            survey.addAnswer("not study",0);
            i++;
        }
        i = 0;
        survey.addAnswer("music",0);
        while (i<3)
        {
            survey.addAnswer("sleep",0);
            i++;
        }
        i = 0;
        survey.addAnswer("doodle",0);
        survey.addQuestion("What is one of the most common things that students do instead of studying?");
        survey.addAnswer("procrastinate",1);
        while (i<7)
        {
            survey.addAnswer("video games",1);
            i++;
        }
        i=0;
        survey.addAnswer("cheat",1);
        while(i<5)
        {
            survey.addAnswer("facebook",1);
            i++;
        }
        i=0;
        while(i<2)
        {
            survey.addAnswer("texting",1);
            i++;
        }
        i=0;
        survey.addAnswer("eating",1);
        while(i<2)
        {
            survey.addAnswer("watch tv",1);
            i++;
        }
        i=0;
        survey.addAnswer("talking",1);
        survey.addAnswer("party",1);
        survey.addQuestion("What are common mistakes when taking a test?");
        while(i<6)
        {
            survey.addAnswer("misreading",2);
            i++;
        }
        i=0;
        while(i<7)
        {
            survey.addAnswer("over analyziing",2);
            i++;
        }
        i=0;
        while(i<2)
        {
            survey.addAnswer("not studying",2);
            i++;
        }
        i=0;
        survey.addAnswer("not writing their name",2);
        while(i<2)
        {
            survey.addAnswer("selecting wrong answer",2);
            i++;
        }
        i=0;
        survey.addAnswer("cheating",2);
        survey.addAnswer("misunderstanding question",2);
        while(i<3)
        {
            survey.addAnswer("Math Errors",2);
            i++;
        }
        i=0;
        while(i<3)
        {
            survey.addAnswer("stressing",2);
            i++;
        }
        i=0;
        survey.addAnswer("forgetting name",2);
        survey.addQuestion("Name an easy way to improve your critical thinking skills");
        while(i<4)
        {
            survey.addAnswer("reading",3);
            i++;
        }
        i=0;
        while(i<2)
        {
            survey.addAnswer("practice",3);
            i++;
        }
        i=0;
        while (i<2)
        {
            survey.addAnswer("Thought provoking games",3);
            i++;
        }
        i=0;
        while (i<4)
        {
            survey.addAnswer("No easy way",3);
            i++;
        }
        i=0;
        while (i<2)
        {
            survey.addAnswer("logic",3);
            i++;
        }
        i=0;
        while (i<2)
        {
            survey.addAnswer("disscusions",3);
            i++;
        }
        i=0;
        survey.addAnswer("listen to music",3);
        survey.addAnswer("attend class",3);
        survey.addAnswer("eating fish",3);
        survey.addAnswer("+crit chance item",3);
        survey.addAnswer("research",3);
        survey.addQuestion("What are things you should not do while studying for an exam?");
        survey.addAnswer("stand on desk and yell",4);
        survey.addAnswer("internet",4);
        survey.addAnswer("no study",4);
        survey.addAnswer("text",4);
        survey.addAnswer("daydream",4);
        survey.addAnswer("beer",4);
        while(i<2)
        {
            survey.addAnswer("facebook",4);
            i++;
        }
        i=0;
         while(i<2)
        {
            survey.addAnswer("video games",4);
            i++;
        }
        i=0;
        while(i<2)
        {
            survey.addAnswer("music",4);
            i++;
        }
        i=0;
        while(i<2)
        {
            survey.addAnswer("Distraction",4);
            i++;
        }
        i=0;
        while(i<2)
        {
            survey.addAnswer("study too long",4);
            i++;
        }
        i=0;
        while(i<2)
        {
            survey.addAnswer("study",4);
            i++;
        }
        i=0;
        while(i<5)
        {
            survey.addAnswer("watch tv",4);
            i++;
        }
        i=0;        
        survey.addQuestion("When reading from a text book, what are good ways to process your information?");
        survey.addAnswer("eyes",5);
        survey.addAnswer("self-quiz",5);
        survey.addAnswer("brain",5);
        survey.addAnswer("Verbally repeating material",5);
        survey.addAnswer("connections",5);
        survey.addAnswer("illustrations",5);
        survey.addAnswer("do examples w.o answers",5);
        survey.addAnswer("food processor",5);
         while(i<7)
        {
            survey.addAnswer("notes",5);
            i++;
        }
        i=0;
         while(i<5)
        {
            survey.addAnswer("re-read",5);
            i++;
        }
        i=0;
         while(i<2)
        {
            survey.addAnswer("highlight",5);
            i++;
        }
        i=0;        
        survey.addQuestion("When you have a large assignment at hand and your time is budgeted what is something you should not do?");
        survey.addAnswer("cocaine",6);
        survey.addAnswer("facebook",6);
        survey.addAnswer("internet",6);
        survey.addAnswer("put it on your foot",6);
        survey.addAnswer("do something else",6);
         while(i<8)
        {
            survey.addAnswer("procrastinate",6);
            i++;
        }
        i=0;
         while(i<2)
        {
            survey.addAnswer("video games",6);
            i++;
        }
        i=0;
         while(i<2)
        {
            survey.addAnswer("watch tv",6);
            i++;
        }
        i=0;
         while(i<2)
        {
            survey.addAnswer("not doing project",6);
            i++;
        }
        i=0;
        survey.addQuestion("What is the most important step when submitting a long paper or essay?");
        survey.addAnswer("not playing video games",7);
        survey.addAnswer("saving",7);
        survey.addAnswer("writing the paper",7);
        survey.addAnswer("selecting topic",7);
        survey.addAnswer("walking in the class",7);
         while(i<9)
        {
            survey.addAnswer("revising/proofreading",7);
            i++;
        }
        i=0;
         while(i<4)
        {
            survey.addAnswer("name on paper",7);
            i++;
        }
        i=0;
         while(i<2)
        {
            survey.addAnswer("submitting",7);
            i++;
        }
        i=0;
    }
    
    public ArrayList<Answer> getAnswers(int index)
    {
        temp = survey.getAnswers(index);
        return temp;
    }
    
    public int getMale()
    {
        return male;
    }
    
    public int getFemale()
    {
        return female;
    }
    
    public int getNumberOfQuestion()
    {
        return numberOfQuestion;
    }
    
    public void incrementNumberOfQuestion()
    {
        numberOfQuestion++;
    }
    
    public void incrementMale()
    {
        male++;
    }
    
    public void incrementFemale()
    {
        female++;
    }



Survey class and Question class and Answer class, how i constructed my survey basically.

public class Survey
{
    private ArrayList<Question> questions;
    private Question question;
    private ArrayList<Answer> temp;
    private String title;

    public Survey(String newTitle)
    {
        title = newTitle;        
        questions = new ArrayList<Question>(0);
    }

    public void addQuestion(String newQuestion)
    {
        question = new Question(newQuestion);
        questions.add(question);
    }

    public void addAnswer(String newAnswer,int questionNumber)
    {
        question = questions.get(questionNumber);
        question.addAnswer(newAnswer);
    }

    public ArrayList<Answer> getAnswers(int index)
    {
        question = questions.get(index);
        temp = question.getAnswers();
        return temp;
    }
    
    public void printAnswers()
    {
        int size = questions.size();
        for(int i =0; i<size; i++)
        {
            question = questions.get(i);
            question.printAnswers();
        }
    }
}


public class Question
{
    private String question;
    private Answer answer;
    protected ArrayList<Answer> answers;
    protected Statistics stats;
    public Question(String newQuestion)
    {
        question = newQuestion;
        stats = new Statistics();
        answers = new ArrayList<Answer>(0);
    }

    public void addAnswer(String newAnswer)
    {
        boolean answerFound = false;
        int size = answers.size();
        for(int i =0;i<size;i++)
        {
            answer = answers.get(i);
            if(answer.getAnswer() == newAnswer)
            {
                answer.incrementCounter();
                answers.set(i,answer);
                answerFound = true;
            }
        }
        if(!answerFound)
        {
            answer = new Answer(newAnswer);
            answers.add(answer);
        }
        stats.incrementNumberOfQuestion();
    }

    public ArrayList<Answer> getAnswers()
    {
        return answers;
    }
    
    public void printAnswers()
    {
        System.out.println(question);
        int size = answers.size();
        for(int i = 0;i<size;i++)
        {
            answer = answers.get(i);
            answer.print();
        }
        System.out.println("Next Question");
        System.out.println(" ");
    }
}





Ok im doing a school presentation where this wasnt even required and the teacher pretty much agrees with what im doing. I wanted to get 100 people to take a survey about study skills because im taking a course called essential study skills. Well i only got around 22 (which is actually pretty good). I decided to just keep it at that create some ground of probabilities then computer generate the rest. The reason is because im going to use these answers to play family feud for my presentation while i explain the real answers from the book along with it.

Also doing this will help calculate all the information needed for the game aswell.

Anyway, im sure youll find the misstake fast. I even did this program the way a somone who is in graduate school says to do it. Test-driven design. I started very basic and slowly built my way up to this and tested every step of the way. But now i got this crazy error and alot of crap going on that might make me have to reprogram the whole thimg.

ALso, Im using BlueJ IDE, i havnt learned any professional ones and since i dont need them for school i dont plan to yet. I will learn netbeans when the time comes for submitting my fun projects to professors to get reviews and advice etc etc etc.
Was This Post Helpful? 0
  • +
  • -

#4 2patrickMurphy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 18-February 12

Re: java.lang.StackOverflowError null

Posted 10 March 2012 - 11:26 PM

public class Answer
{
   private String answer;
   private int counter;
   public Answer(String newAnswer)
   {
       answer = newAnswer;
       counter++;
   }    
   public void incrementCounter()
   {
       counter++;
   }
   
   public void print()
   {
       System.out.println("Answer: " + answer + " " + "Counter " + counter);
    }
    public String getAnswer()
    {
        return answer;
    }
    public int getCounter()
    {
        return counter;
    }
  
}


Guess i forgot to put this in here. Although im highly positive it wont matter
Was This Post Helpful? 0
  • +
  • -

#5 2patrickMurphy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 18-February 12

Re: java.lang.StackOverflowError null

Posted 11 March 2012 - 12:52 AM

 /**
 *     
    the beginning 

java.lang.StackOverflowError
	at Survey.<init>(Survey.java:18)
	at Statistics.<init>(Statistics.java:51)
	at Question.<init>(Question.java:17)
	at Survey.addQuestion(Survey.java:23)
	at Statistics.<init>(Statistics.java:52)
	at Question.<init>(Question.java:17)
	at Survey.addQuestion(Survey.java:23)
	at Statistics.<init>(Statistics.java:52)
	at Question.<init>(Question.java:17)
	at Survey.addQuestion(Survey.java:23)
	at Statistics.<init>(Statistics.java:52)
	at Question.<init>(Question.java:17)
	at Survey.addQuestion(Survey.java:23)
	at Statistics.<init>(Statistics.java:52)
	at Question.<init>(Question.java:17)
	at Survey.addQuestion(Survey.java:23)

towards the end.

at Question.<init>(Question.java:17)
	at Survey.addQuestion(Survey.java:23)
	at Statistics.<init>(Statistics.java:52)
	at Question.<init>(Question.java:17)
	at Survey.addQuestion(Survey.java:23)
	at Statistics.<init>(Statistics.java:52)
	at Question.<init>(Question.java:17)
	at Survey.addQuestion(Survey.java:23)
	at Statistics.<init>(Statistics.java:52)
	at Question.<init>(Question.java:17)
	at Survey.addQuestion(Survey.java:23)
	at Statistics.<init>(Statistics.java:52)
	at Probability.<init>(Probability.java:19)
	at Tester.<init>(Tester.java:22)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at bluej.runtime.ExecServer$3.run(ExecServer.java:740)

*/

Was This Post Helpful? 0
  • +
  • -

#6 2patrickMurphy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 18-February 12

Re: java.lang.StackOverflowError null

Posted 11 March 2012 - 01:18 AM

problem fixed, In my constructor for statistics i call on the methoad addQuestion()

I removed that from my constructor and made it a seperate method which is then called on by the tester class.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1