• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to store a user input file path in a variable  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay so I'm trying to store a user input File path ( Example: c:/Hello/random.txt ) in a public static String it gives no errors but the String is null.




So I realize there are issues with that code, not errors but some things are not necessary some thigns are missing lol etc.. I'm coming to java from c++ so I'm finding some things difficult to get the hang of.

Any way as I said above I'm trying to store a file path in public static String FULL_PATH but it's not working properly and if I attempt to open the file and write to it I would get a Java.lang.NullPointerException.

Unfortunately these code snippets are just that If I put all the source here it would be like 2000 lines every thing else works the main issue is as I said just opening and writing to the file.

 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

One of our mods, Winston Gutkowski, says you should never have a static variable. I am sure he is right. And you should most definitely not have public variables, because those are equivalent to global variables and they are very error‑prone. Anyway, create an object which reads from that file, and set up the path variable in its constructor. Don't pass the path to a different class.Please find out the conventions for names: only class/interface/enums should start with CapitalLetters, and only use underscores in _package_names or CONSTANT_NAMES. This link is old and no longer maintained actively, but it tells you the naming conventions.

Using File is now considered old‑fashioned programming. Read about the newer classes in the Java™ Tutorials.
 
Doug Barnes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I wasn't really comfortable with doing it the way I did I generally avoid global variables with very few exceptions Thank you so much for the help!
 
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I have never seen Java code being indented in this way. Is this intended or is this some weirdness from your IDE? This way it looks quite a lot like the top half of the Arrow Anti-Pattern. You might want to fix this as well.
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. Proper indentation isn't just a nicety. It's not only necessary to make your code readable, it's an impotent tool to reveal structuring problems with your code.
 
Doug Barnes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:And I have never seen Java code being indented in this way. Is this intended or is this some weirdness from your IDE? This way it looks quite a lot like the top half of the Arrow Anti-Pattern. You might want to fix this as well.


Indenting code is only suggested it's not actually a requirement it's a nicety and I'm used to doing it a certain way which is not to say that my way is the best way but it's certainley not the worst either in fact many professional programmers, colleges etc.. teach you to double indent, and I'm used to indenting code such as this.



I mean if you look at c++ or qt forums this is what you will see I'm not here to argue with any one I've said that I'm new to java and I'm sure you guys have your own preferred methods of indentation which I am more then happy to learn but I can't fail to remind every one that it's not a requirement lol I try my best to make my code readable in general and more so when I post things on sites asking for help it's not hard to read or understand.
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doug Barnes wrote:Indenting code is only suggested it's not actually a requirement it's a nicety

No, it really isn't! Using appropriate indentation is way more important than you are thinking right now.

Doug Barnes wrote:I try my best to make my code readable in general and more so when I post things on sites asking for help it's not hard to read or understand.

Your code is not readable at all! In fact, because this code is hard(er) to read, it might cause people not to reply to your post because it requires too much effort to read and understand what's going on. And because of how your code is indented, it could be that you are not posting the actual code snippet, but you removed an if statement (or loop) without adjusting the indentation, and thus the actual issue/bug/problem in your code could be in the code you have deleted.

As you are a new to Java, you might have a read of the Code Conventions. You should use no underscores in variable names and should have a camelCase name (unless if it's a constant), so inputTwo instead of Input_Two. And method names also follow the camelCase naming.

And of course for your own development you can choose whatever indentation and naming you are comfortable with. But once you are part of the team, you'll have to follow the code conventions which are applicable in the team. Because such conventions make code easier to read and understand, even if the code is written by several different developers. And it's also much easier to compare code listings and only focus on actual code changes and not see a truckload of whitespace changes.
Different companies and/or teams will have some slight differences in their coding conventions (because there's no general rule or requirement as you stated), but I have never encountered formatting like yours (not even when I was developing in C, C++ and VB6/.Net). If a developer in my team tries to use formatting/indenting like that, I will tell him exactly the same as I mentioned here and I'll provide him the formatter we are using and ask him/her to be so kind to format the code using that formatter before he submits the code to the source code repository.

As a final remark: class Vector is a very old one (since JDK 1.0) and it's almost not used at all anymore. You want to use List (and ArrayList), as your variable name already suggests

Hope it helps!
Kind regards,
Roel
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doug Barnes wrote:Indenting code is only suggested it's not actually a requirement

I could not agree with Roel more. It is a requirement. Not following a conventional and rational indentation style would certainly fail any code read at any place I've worked at throughout my career (37 years worth).

it's a nicety and I'm used to doing it a certain way which is not to say that my way is the best way

As I pointed out before, not only is it a readability issue, hardly a nicety, but also an important structural tool. Your way, is not rational in any sense. What is it that you think that your odd indentation style provides? It makes your code unreadable and the structure impossible to decipher.

but it's certainley not the worst either in fact many professional programmers, colleges etc.. teach you to double indent

I'm not sure what you mean by that, but there isn't any way someone (who knows what they are talking about) would teach your style.

and I'm used to indenting code such as this.

If you are here to learn, then this is something you'll need to unlearn pretty quickly. Is it not rational, readable, and would not be acceptable in any professional organization with which I have been acquainted.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!