• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difficulty in creating an object despite it being declared?  RSS feed

 
tony narloch
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi guys.

I keep getting an error with the line of code: 



Keep being informed:

"cannot find symbol -   class accountManager"

But I cannot for the life of me, work this out at all

I thought I had properly declared used the constructor etc, but I cannot for the life of me understand where I am going wrong?



 
John Joe
Ranch Hand
Posts: 439
3
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look like you want to call AccountManager class. Replace your code to this
Note that accountManager is an object, not class.
 
tony narloch
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Joe wrote:Look like you want to call AccountManager class. Replace your code to this
Note that accountManager is an object, not class.


Unfortunately that has returned error: cannot find symbol - variable accountManager
 
John Joe
Ranch Hand
Posts: 439
3
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tony narloch wrote:Unfortunately that has returned error: cannot find symbol - variable accountManager
What is the error after you changed accountManager to AccountManager ?
 
tony narloch
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I posted my reply too quickly: realised that by altering the code at the bottom, I had to then alter all other instances of it in the code. I wanted to edit my previous post, but am unable to do so.

Sadly, There is a new problem being flagged up:

constructor AccountManager in class lab9.AccountManager cannot be applied to given types;
  required: lab9.Account,java.util.Scanner,lab9.Transaction,lab9.AccountManager
  found: no arguments
  reason: actual and formal argument lists differ in length

I tried the following:


i.e, I entered the required arguments as specified verbatim.

Doing this resulted in an error message :"cannot find variable" for each of the four parameters.

I modified the code to match the variable names in the field declaration at the start of the class as follows:



This reduced the total number of errors from 4 to 2.

The two values that generated the errors are:



I wanted to be as precise and exhaustive as possible (To show that I AM actually trying)

 
tony narloch
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duplicate post made in error, sorry mods
Content minimized. Click to view
 
John Joe
Ranch Hand
Posts: 439
3
Android Eclipse IDE Java
  • Likes 1 Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
declare Scanner reader as static
 
tony narloch
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Regrettably, same two errors are being produced
 
tony narloch
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now I am advised:



I am a little confused by this, I know I have declared accountManager, but I thought I had also initialized it with the line:

 
John Joe
Ranch Hand
Posts: 439
3
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to pass object accountManager, create another constructor instead
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tony narloch wrote:. . . I thought I had properly declared used the constructor etc, but I cannot for the life of me understand where I am going wrong?
I can see threefour errors:-
  • 1: As John Joe noticed, you are writing accountManager rather than AccountManager. The a and A are different.
  • 2: Your AccountManager class doesn't seem to have a no‑arguments constructor. You cannot use AccountManager(), but must supply four arguments.
  • 3: The AccountManager class has itself as a field, so there is a risk of your creating an infinite recursion. I think maybe you should remove that field. Or at least explain why it is there in the first place.
  • 4: Remove the Scanner argument from the constructor. I shall leave you to work out what the error there is.
  • Avoid creating multiple constructors.
    You are being given bad advice, and that is causing you to create new errors rather than correcting old errors. Making things static is usually a serious error. What is going to happen when you create a new transaction?

    [edit]Change three errors to four and change format about error about the Scanner in the constructor.
     
    John Joe
    Ranch Hand
    Posts: 439
    3
    Android Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Campbell, can you explain to me so I can learn from mistake ?

    1. Why we cannot declare things as static ?
    2. Why we should avoid creating multiple constructors?

    Thanks

     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There are some things which can be static. But they need a good explanation. You have provided no explanation for making the Scanner reference static, and that has confused the OP into making the transaction object static too. Your advice has misled him.
    If you create multiple constructors, you will allow the account manager object to be created and remain in an inconsistent state. If you use a no‑args constructor all the fields of that object will point to null, and that will be incorrect.

    I am seeing what I see elsewhere: trying to alter correct code to the compiler error, even when the compiler error shows the correct location of the error.
    Cannot find symbol accountManager: you correctly located the error to the line where the compiler error occurred and gave a correct solution.
    When it then start complaining about not finding a constructor AccountManager(), the compiler shows the correct location of the error. The solution is to correct the line where the error occurred, i.e. trying to invoke a no‑args constructor. Creating a no‑args constructor to try and correct the compiler error simply introduces logic errors which will cause trouble later. At least there is a good chance that you will suffer an exception from those errors; some logic errors simply run and produce a wrong answer. That is a far more serious error.
     
    tony narloch
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    I am getting the error message:

    non-static variable transaction cannot be referenced from a static context, which I believe is due to the fact that it does not exist in the main procedure, because it's static, which means it cannot access instance-level members so I would need to create an instance of it.

    I have modified the code accordingly:



    Is that valid?

    An error is being generated for accountmanager: "variable accountmanager might not have been initialized"
     
    tony narloch
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My understanding here is that whilst I have declared the accountmanager variable, I have not initialised it, and to do so I would need to assign it to something:

    accountmanager =10;

    However, my confusion lies in what exactly I should specify the value of accountmanager to be
     
    Knute Snortum
    Sheriff
    Posts: 4281
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In general, the main() method should have only one or two lines in it.  See MainIsAPain (that's a link).
    or even just
    Then move the code in main() to run():
    I would also move the whole main() and run() methods into a new class called AccountManagerTest.
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    tony narloch wrote:. . . accountmanager =10;
    I thought it was accountManager. You have to assign it to something, but not 10 because it is the wrong type of value.


    However, my confusion lies in what exactly I should specify the value of accountmanager to be
    You will have to decide what accountManager should be. You are writing the program, you should know what the different variables are supposed to mean. I still think you have too many accountManagers around.
     
    tony narloch
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    Did you mean like this?



    AccountManager accountmanager= new AccountManager (account,t, accountmanager); has the error "self-reference in initializer"
    /constructor AccountManager in class lab9.AccountManager cannot be applied to given types;
      required: lab9.Account,lab9.Transaction,lab9.AccountManager
      found: no arguments
      reason: actual and formal argument lists differ in length
    error: cant find method run
     
    Knute Snortum
    Sheriff
    Posts: 4281
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Not quite:
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can shorten that
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I see Knute has already shown you the shorter version.
     
    tony narloch
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Knute Snortum wrote:Not quite:


    I am going to include an excerpt from my assignment.



    I am using BlueJ for my project.

    I have created a new project. I have copied your code exactly, and I am getting numerous errors, all about not finding the symbol for the variables. I assume this is due to the abscence of a constructor, is there a reason you omitted it? I am so confused now

     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How did you get line 18 to compile? It doesn't look right to me. I have already commented about that accountManager object.
     
    tony narloch
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    I made some changes to the code. Tried to refactor it.
     
    tony narloch
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    String inputAmount = reader.nextLine();
    am.start();

    Both of these lines are generating "java.lang.NullPointerException"

    Which my very limited understannding of Java means....that one of my variables is null? But I do not see any null references?
     
    tony narloch
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The only segment of code I can see with null



    But again, this code compiled successfully...so at a loss now
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Line 4: Don't use the Date class. A dreadful thing to use. Go through the Java™ Tutorials to find out what to use instead.
    Lines 7-9, 25, etc: Write something “real” in those documentation comments. I think you should have documentation comments on everything for an assignment. But that is only my opinion.
    Lines 14, 16, 25: Delete those comments which don't tell you anything you don't know already.
    Lines 19-22, 32-33, 35-37, etc: Get rid of those repeated empty lines. They don't help legibility.
    Line 30: Work out what the serious error is in this line. But it won't produce any wrong results, because of the exceptions.
     
    Knute Snortum
    Sheriff
    Posts: 4281
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    tony narloch wrote:String inputAmount = reader.nextLine();
    am.start();

    Both of these lines are generating "java.lang.NullPointerException"

    Which my very limited understannding of Java means....that one of my variables is null? But I do not see any null references?

    It's true that an NPE means some variable is null.  But a variable can be null even if you don't set it to null.

    Please post the complete error message with the stack trace.  I'm not sure how that works in BlueJ, though.
     
    tony narloch
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    java.lang.NullPointerException
    at lab9.AccountManager.start(AccountManager.java:64)
    at lab9.AccountManager.main(AccountManager.java:110)
     
    Knute Snortum
    Sheriff
    Posts: 4281
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And what does line 64 look like?  Because the line 64 of the code you posted is blank.
     
    tony narloch
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    lab9.AccountManager.start(AccountManager.java:64):  

    at lab9.AccountManager.main(AccountManager.java:110)

    That is from the terminal, rather than debugger; but I did notice something odd, the Scanner reader =null, throughout the whole of the program


     
    tony narloch
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi;

    Decided to cut my losses. I am now so stressed with this I am in tears, so I am going to hand the work in as is, and take my chances.

    Have been working on this for nearly 4 days straight now, and am utterly fed up.

    Thank you for your patience and support guys.
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Knute Snortum wrote:. . . "java.lang.NullPointerException" . . . .
    I told you hours ago that line 30 would cause exceptions. Look there.
     
    tony narloch
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, you did. I was just baffled at how I was to fix it.
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Start by comparing lines 29 30 and 31. See if you can see the difference between them.
     
    tony narloch
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes.

    Um, it should be:


    ?
     
    tony narloch
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    Remove static to get




    Declare and initialize in public static void main



    ?
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    tony narloch wrote:Yes.

    Um, it should be:


    ?
    Apart from the formatting: Yes, that will work It also means you don't need the Scanner parameter in the constructor.

    Once you get that to work, you will have something you can hand in and get marks for. Stop while you are ahead. I suggest you shouldn't try any more changes there, otherwise you will end up wasting time which you could use to get more marks elsewhere.

    You usually need one Scanner pointing to System.in per program, not per object, so I recommend a utility class to wrap that Scanner object in. So you never actually see the Scanner object, only the utility class. But you don't have enough time to implement that now. So don't try it now.
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    By the way: never close a Scanner reading from System.in.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!