• 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

Illegal Start of the "try" Block?!

 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a program with try, catch, finally blocks in it. I do not understand why the compiler says that
1. UserDirectory.java:34: Illegal start of type try
(line 34 is flagged in the code below)
2. UserDirectory.java:84: <identifier> expected }
(line 84 is also flagged in the code below)
Please help.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The code between line 34 and line 84 is outside of any method or initialization block, so the compiler assumes "try" is supposed to be the return type of a method. You've got methods declared inside the try block, too, which are also illegal; you can only define methods at class scope, and you can only put code (try blocks, for example) inside a method or an initialization block.
I'm really confused about what you're trying to do in a lot of places:
  • Who is supposed to call the isUserLogon() method?
  • At line 32, you initilize a member _username with a call to fixId() passing the member username as an argument. But username isn't initialized in the constructor, so when this code runs, you'll get a NullPointerException.
  • What should initiate the execution of the code in the big try block?

  • I hope I'm not insulting you, but I'm guessing you're not an experienced Java programmer, yes? You might want to start out with an introductory Java book like
    Head First Java before jumping into something as involved as working with Struts (this is derived from something in Struts in Action, yes?)
     
    JiaPei Jen
    Ranch Hand
    Posts: 1309
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you, Ernest. You correctly pointed out my problems. I do not have much hands-on experience in IT. Nonetheless, I am learning a lot (in Java, Servlet, JSP, and Struts) by constructing a web site.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic