Home > Archive > Java Help > May 2004 > What is this error?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
What is this error?
|
|
|
| Hello,
We've been working on this program for the past w as a project in
our Computer Programming class, and this has the teacher stumped.
Here are the file links:
http://leaf.iconrate.net/POSTrivia.java
http://leaf.iconrate.net/FileInput.java
http://leaf.iconrate.net/answers.txt
http://leaf.iconrate.net/questions.txt
What happens is that whenever we instatiate the FileInput class, it
says something about wanting to throw a FileNotFoundException, no
matter where we instantiate it. Whenever the part of the FileInput
class with the BufferedReader is called, that is when the error is.
Then, when we try to throw the exception at a proper place, it says
that it can't file the FileNotFoundException class, or something
similar.
If you can figure anything out, or need more info, please e-mail:
aaron.axvig@sendit.nodak.edu
Aaron, Mike, and Shawn
| |
| Gordon Beaton 2004-05-12, 7:55 pm |
| On 12 May 2004 07:15:32 -0700, Aaron wrote:
> We've been working on this program for the past w as a project in
> our Computer Programming class, and this has the teacher stumped.
> Here are the file links:
>
> http://leaf.iconrate.net/POSTrivia.java
> http://leaf.iconrate.net/FileInput.java
> http://leaf.iconrate.net/answers.txt
> http://leaf.iconrate.net/questions.txt
>
> What happens is that whenever we instatiate the FileInput class, it
> says something about wanting to throw a FileNotFoundException, no
> matter where we instantiate it. Whenever the part of the FileInput
> class with the BufferedReader is called, that is when the error is.
> Then, when we try to throw the exception at a proper place, it says
> that it can't file the FileNotFoundException class, or something
> similar.
If something that trivial has got your teacher stumped, I'd say you
need a new teacher.
The FileInput constructor declares that it can throw a
FileNotFoundException, so you need to deal with that possibility in
the code where you use it, either by handling it (catching it) or
declaring that your method can also throw that exception.
If the compiler can't find the class, it's because you need to import
the appropriate package.
> If you can figure anything out, or need more info, please e-mail:
You posted here, you read the answer here.
/gordon
--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
| |
| Roedy Green 2004-05-12, 7:55 pm |
| On 12 May 2004 07:15:32 -0700, aaron.axvig@sendit.nodak.edu (Aaron)
wrote or quoted :
>Then, when we try to throw the exception at a proper place, it says
>that it can't file the FileNotFoundException class, or something
>similar.
After that can you help me with my program? It gives an error message
something about the program not working. What did I do wrong?
Come on, QUOTE PRECISELY if you expect people to help you.
See http://mindprod.com/jgloss/errormessages.html
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
| |
| Aaron 2004-05-13, 10:32 am |
| > After that can you help me with my program? It gives an error message
> something about the program not working. What did I do wrong?
>
> Come on, QUOTE PRECISELY if you expect people to help you.
OK, I will try to explain better, since I was rushed before.
The FileInput class uses BufferedReaders, which are in the default
constructor. The constructor throws FileNotFoundException and
IOException. Note that this class will compile successfully.
The problem arises when we try to instantiate and use the FileInput
class. We want to have the object that is an instance of the
FileInput class as a global variable, declared within the class, and
not in a method. So, how is a FileNotFoundException supposed to be
thrown for a class?
We have tried numerous workarounds. First, the BufferedReader section
was relocated to a method in the FileInput class. Then, that method
was called after the FileInput class was instantiated. Then, it said
that we needed to throw a FileNotFoundException for the method in
which we called the method with BufferedReader in it. So of course,
we added that code, and the compiler says that it cannot resolve the
symbol FileNotFoundException. All the places that might work to have
this code there say the same thing. Does FileNotFoundException have
to be imported? We've done numerous programs using that without
importing it, so why now?
The method containing the BufferedReader does throw a
FileNotFoundException; does that mean whatever calls that also needs
to have a FileNotFoundException?
I hope those details help a little more.
Aaron, Mike, Shawn
| |
| Andrew Thompson 2004-05-13, 11:40 am |
| On 13 May 2004 06:45:08 -0700, Aaron wrote:
...answers below, first, some comments
> OK, I will try to explain better, since I was rushed before.
Your time is more valuable than
ours, ..is that it?
If you do not have the time to prepare
the quesstion properly, please do not
spend our time and bandwidth.
> We've been working on this program for the past
> w as a project in our Computer Programming class,
> and this has the teacher stumped.
Comments: Either you have stretched
the truth, or your teacher is an idiot.
What school do you attend?
For the teacher to have not known how
to fix this problem indicates that they
are utterly incompetent at teaching Java.
> Here are the file links:
It is good you provided links..
The total of the code comes to 11Kb,
also pretty crisp, but you could trim
the size of the UI as most of it
(no ..all, actually) was unrelated
to the actual problem.
You could have done your entire example
in a single Java file if under 6Kb.
Check here for tips on code trimming
<http://www.physci.org/codes/sscce.jsp>
Then we come to..
// ========================================
==============
// Source code generated by jvider v1.2.3 UNREGISTERED version.
// http://www.j vider.com/
// ========================================
==============
...you teacher encourages.. or allows you
to use a GUI generator? It looks like a fine
little program (as far as GUI generators go,
they almost all suck), but I ask you to
consider a different approach.
<http://www.xdweb.net/~dibblego/java...nswers.html#q34>
That advice applies, possibly more so, to GUI's.
Again, the teacher has made a poor decision.
Now, finally, we come to the problem!
You seem to be doing advanced things like
generating GUI's before you understand simple
things like variable scope and exception handling.
The actual code that is a problem is a section
in POSTTrivia.java..
<snippet>
{
......
FileInput questionRetriever=new FileInput();
//---------------------------------------------------------------------------
public TriviaPanel() {
super();
setBorder(BorderFactory.createTitledBorder("POS Trivia Machine"));
GridBagLayout gbTriviaPanel = new GridBagLayout();
GridBagConstraints gbcTriviaPanel = new GridBagConstraints();
setLayout(gbTriviaPanel);
rbgTriviaPanel = new ButtonGroup();
</snippet>
You have declared FileInput as a class level attribute,
but you cannot instantiate it at the same time, unless
your constructor throws an Exception.
There are various ways to fix this, try/catch in
main might be the best, but I will show you another..
<snippet>
FileInput questionRetriever;
//---------------------------------------------------------------------------
public TriviaPanel() {
super();
setBorder(BorderFactory.createTitledBorder("POS Trivia Machine"));
GridBagLayout gbTriviaPanel = new GridBagLayout();
GridBagConstraints gbcTriviaPanel = new GridBagConstraints();
setLayout(gbTriviaPanel);
rbgTriviaPanel = new ButtonGroup();
try { questionRetriever=new FileInput(); }
catch(Exception e) { e.printStackTrace(); }
// all bets are off now!
....
</snippet>
This is short, but not advisable as we ignore
the error entirely and continue. This will probably
cause the program to fail in a less predictable way,
later in the program. However, it starts to
demonstrate the way to a solution.
There is another 'gotcha' here, the TriviaPanel
is an inner class, that may be adding to the confusion.
(Another reason not to use automatic code generators,
if you write it yourself, you understnand what is
happening)
All up, you should have a good
long through the FAQ.
Check here for tips..
<http://www.physci.org/codes/javafaq.jsp#start>
<http://www.physci.org/codes/javafaq.jsp#specific>
<http://www.physci.org/codes/javafaq.jsp#exact>
Also, it is important to understand that if
you go shooting off too many poorly framed
questions in haste, people will quickly stop
helping. Please put more thought and effort
into your questions in future.
--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
| |
| Chris Smith 2004-05-13, 2:34 pm |
| Gordon Beaton wrote:
> If something that trivial has got your teacher stumped, I'd say you
> need a new teacher.
Agreed.
> The FileInput constructor declares that it can throw a
> FileNotFoundException, so you need to deal with that possibility in
> the code where you use it, either by handling it (catching it) or
> declaring that your method can also throw that exception.
Looking over the code, it appears that FileInput is instantiated in an
instance variable initializer. That means that *all* constructors in
the class must declare those exceptions (FileNotFoundException and
IOException). And yes, import them so that they will be found.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
| |
| Bjorn Abelli 2004-05-13, 2:34 pm |
| "Aaron" wrote...
> The FileInput class uses BufferedReaders, which are
> in the default constructor. The constructor throws
> FileNotFoundException and IOException. Note that this
> class will compile successfully.
That doesn't mean that the solution can't be to change that class!
> The problem arises when we try to instantiate
> and use the FileInput class. We want to have
> the object that is an instance of the FileInput
> class as a global variable, declared within the
> class, and not in a method.
> So, how is a FileNotFoundException supposed to be
> thrown for a class?
Either you *don't* throw it, but *handle* it within the FileInput class, or
you see to that any thrown exceptions *is* handled at the instantiation of
it.
In your code you had it as a static variable...
OK, you can do that, but then handle the instantiation within a method or a
static initializer, so you can catch the Exception.
> Does FileNotFoundException have to be imported?
If you reference it in some way, yes. It's in the java.io-package.
> The method containing the BufferedReader does throw a
> FileNotFoundException; does that mean whatever calls
> that also needs to have a FileNotFoundException?
Any methods that don't *handle* such exceptions will fail to compile. To
handle an exception means that either you handle it *within* the method,
with a try-catch, or that the method just throws it further up the calling
stack (as you tried to do with the *constructor*). In the latter case you
must handle it in *that* calling method instead, etc...
// Bjorn A
|
|
|
|
|