• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Populate Array of type Double from text file

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:I'd still like you to tell me what's on the lines of code indicated in this stack trace (if you haven't changed things so that those line numbers no longer apply):



Line 201: monthlyInterest = (interest[termList.getSelectedIndex()] / (12 * 100));
Line 19: public class PaymentCalculatorWeek5 extends JPanel {
Line 64: button1ActionPerformed(evt);
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brandon Buchanan wrote:

Jeff Verdegan wrote:Do you know what the void means there? Do you know how to return a value from a method?


To be honest, I don't FULLY understand it.


Okay. the void means that the method doesn't return any value to the calling code.
However, you need to change your method so that it does return a value. So you need to change the void to the type of the value that you are going to return. You are returning an array of doubles, so you need to change it to

Then at any point where your method returns, you will have to return a double array or null. I believe your method only returns when it gets to the end, so the last line of your method needs to be
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brandon Buchanan wrote:

Jeff Verdegan wrote:In this case, though, I'm going to guess that you only changed that, but did not change your getInterestRates() method.


OK, I made that change as well and here are the errors that I get:



The error messages are telling you exactly what's wrong. Read it carefully, and look carefully at the declaration of getInterestRates() and the declaration of interest, and at the code it's complaining about.

Also, I think I may have been careless and made a small mistake in some of the advice I gave you. That may have led to these errors. Think about what those error messages mean.

Jeff Verdegan wrote:Do you know what the void means there? Do you know how to return a value from a method?


To be honest, I don't FULLY understand it.

It looks like you have a pretty good idea, and you're probably just second-guessing yourself (and possibly putting too much faith in the exactitude of my advice )

In short, void means the method doesn't return a value, so you can't do something = thatMethod(). If you want that method to return a value, you declare it to return the type you want, and then you add a return whatever statement to the method. For example:


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brandon Buchanan wrote:

Jeff Verdegan wrote:I'd still like you to tell me what's on the lines of code indicated in this stack trace (if you haven't changed things so that those line numbers no longer apply):



Line 201: monthlyInterest = (interest[termList.getSelectedIndex()] / (12 * 100));
Line 19: public class PaymentCalculatorWeek5 extends JPanel {
Line 64: button1ActionPerformed(evt);



Okay, the anonymous inner class makes this a bit confusing, but basically, other than it's reference to the beginning of the class, the stack trace tells us, from the bottom up, what called what at which line of code. Ultimately, at the top, line 201, we see that we're doing monthlyInterest = (interest[termList.getSelectedIndex()] / (12 * 100));, and this is where the error occurred. Looking at this line, we can see that either interest or temrList must be null, (or possibly interest[termList.getSelectedIndex()], if interest is Double[] rather than double[]).

We had already suspected it was interest, but this tells us concretely what might possibly be null, and learning how to use the stack trace this way is vital for debugging.
 
Brandon Buchanan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Okay, the anonymous inner class makes this a bit confusing, but basically, other than it's reference to the beginning of the class, the stack trace tells us, from the bottom up, what called what at which line of code. Ultimately, at the top, line 201, we see that we're doing monthlyInterest = (interest[termList.getSelectedIndex()] / (12 * 100));, and this is where the error occurred. Looking at this line, we can see that either interest or temrList must be null, (or possibly interest[termList.getSelectedIndex()], if interest is Double[] rather than double[]).



Well termList is not Null because I hardcode those values into the drop down box here:

So I am thinking that those values are never reaching the main interest array that I declare at the beginning. It seems to me that it could be a placement problem, like where I am reading the file, and that it's not able to access it in other places, is that what you are thinking?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brandon Buchanan wrote:

Jeff Verdegan wrote:Okay, the anonymous inner class makes this a bit confusing, but basically, other than it's reference to the beginning of the class, the stack trace tells us, from the bottom up, what called what at which line of code. Ultimately, at the top, line 201, we see that we're doing monthlyInterest = (interest[termList.getSelectedIndex()] / (12 * 100));, and this is where the error occurred. Looking at this line, we can see that either interest or temrList must be null, (or possibly interest[termList.getSelectedIndex()], if interest is Double[] rather than double[]).



Well termList is not Null because I hardcode those values into the drop down box here:



Since I can't see the entire code (and probably wouldn't bother to read it if I could), I can't be sure that that termList variable is the same one in the line in question. Even if it was my code, I would still check my assumption. I would put print statements right before the line where the error occurs to print out everything that syntactically could be null at that point, even if I was sure I had set it.

So I am thinking that those values are never reaching the main interest array that I declare at the beginning. It seems to me that it could be a placement problem, like where I am reading the file, and that it's not able to access it in other places, is that what you are thinking?



I think we already found the source of the problem, and you're already on your way to fixing it--namely to return a double[] from getInterestRates() and assign that to your interest variable. (Not sure if I have the names right, but you know what I mean, I hope.) This stuff was to help you learn how to use the stack trace to find the source of the problem (and to confirm our hunch, which it has pretty much done).
 
Brandon Buchanan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:I think we already found the source of the problem, and you're already on your way to fixing it--namely to return a double[] from getInterestRates() and assign that to your interest variable. (Not sure if I have the names right, but you know what I mean, I hope.) This stuff was to help you learn how to use the stack trace to find the source of the problem (and to confirm our hunch, which it has pretty much done).



I went out on a limb and tried to use scanner and for some reason that clicked and ended up working for me! w00t!! And, in doing so I see where some of my logic could have been messed up in the way we were doing it before, so I could probably go back and fix it, but since I have it working, I probably wont!

Thanks again for all your help, this has been an amazing learning experience.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! Glad you got it working! I know it was a frustrating journey, but I'm glad you kept at it.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic