Common java program errors

Top 8 Common Java Errors for Beginners — and How to Solve Them

Did you know that in Java’s standard library, there are a total of more than 500 different exceptions! There lots of ways for programmers to make mistakes — each of them unique and complex. Luckily we’ve taken the time to unwrap the meaning behind many of these errors, so you can spend less time debugging and more time coding. To begin with, let’s have a look at the syntax errors!

Syntax Errors

If you just started programming in Java, then syntax errors are the first problems you’ll meet! You can think of syntax as grammer in English. No joke, syntax errors might look minimal or simple to bust, but you need a lot of practice and consistency to learn to write error-free code. It doesn’t require a lot of math to fix these, syntax just defines the language rules. For further pertinent information, you may refer to java syntax articles.

Working with semicolons (;)

Think of semi-colons (;) in Java as you think of a full-stop (.) in English. A full stop tells readers the message a sentence is trying to convey is over. A semi-colon in code indicates the instruction for that line is over. Forgetting to add semi-colons (;) at the end of code is a common mistake beginners make. Let’s look at a basic example.

Читайте также:  Для удобства создания веб-страниц рекомендуется использовать HTML-редактор

This snippet will produce the following error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error, insert ";" to complete BlockStatements at topJavaErrors.JavaErrors.main(JavaErrors.java:3)

You can resolve this error by adding a ; at the end of line 3.

Braces or parentheses [(), <>]

Initially, it can be hard keeping a track of the starting / closing parenthesis or braces. Luckily IDEs are here to help. IDE stands for integrated development environment, and it’s a place you can write and run code. Replit for example, is an IDE. Most IDEs have IntelliSense, which is auto-complete for programmers, and will add closing brackets and parentheses for you. Despite the assistance, mistakes happen. Here’s a quick example of how you can put an extra closing bracket or miss the ending brace to mess up your code.

If you try to execute this, you’ll get the following error.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error on token ")", delete this token at topJavaErrors.SyntaxErrors.main(SyntaxErrors.java:11)

You can resolve this exception by removing the extra ) on line 9.

My full name is: Justin Delaware 

Double Quotes or Quotation Marks (“ ”)

Another pit fall is forgetting quotation marks not escaping them propperly. The IntelliSense can rescue you if you forget the remaining double quotes. If you try including quotation marks inside strings, Java will get confused. Strings are indicated using quotation marks, so having quotation marks in a string will make Java think the string ended early. To fix this, add a backslash (\) before quotation marks in strings. The backslash tells Java that this string should not be included in the syntax.

Exception in thread "main" java.lang.Error: Unresolved compilation problems: Syntax error on token "Java", instanceof expected The preview feature Instanceof Pattern is only available with source level 13 and above is cannot be resolved to a type Syntax error, insert ")" to complete MethodInvocation Syntax error, insert ";" to complete Statement The method favourtie(String) is undefined for the type SyntaxErrors Syntax error on token "language", ( expected at topJavaErrors.SyntaxErrors.main(SyntaxErrors.java:5)

In order for you avoid such exceptions, you can add backslahes to the quotes in the string on line 4.

What did Justin say? Justin said, "Java is my favourite language"

Here’s your required output, nicely put with double quotes! 🙂

Other Miscellaneous Errors

Accessing the “Un-Initialized” Variables

If you’re learning Java and have experience with other programming languages (like C++) then you might have a habit of using un-initialized variables (esp integers). Un-initialized variables are declared variables without a value. Java regulates this and doesn’t allow using a variable that has not been initialized yet.

If you attempt to access an uninitialized variable then you’ll get the following exception.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: The local variable contactNumber may not have been initialized at topJavaErrors.UninitialziedVars.main(UninitialziedVars.java:5)

You can initialize the variable “contactNumber” to resolve this exception.

int contactNumber = 9935856;

Accessing the “Out of Scope” Variables

If you define a variable in a certain method you’re only allowed to access that in the defined scope of it. Like each state has their legitimate currency, and that cannot be used in another state. You cannot use GBP in place of USD in America. Similarly, a variable defined in one method has restricted scope to it. You cannot access a local variable defined in some function in the main method. For further detailed illustration let’s look at an example.

As soon as you run this snippet, you’ll get the exception

Exception in thread "main" java.lang.Error: Unresolved compilation problem: country cannot be resolved to a variable at topJavaErrors.OutOfScopeVars.main(OutOfScopeVars.java:9)

You can not access the variable “country” outside the method getPersonalDetails since its scope is local.

Modifying the “CONSTANT” Values

Java and other programming languages don’t allow you to update or modify constant variables. You can use the keyword “final” before a variable to make it constant in Java. Apart from that, it’s a convention to write a constant in ALL CAPS for distinction purposes, As a constant resource is often used cross methods across a program.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: The final field ConstVals.SSN cannot be assigned at topJavaErrors.ConstVals.main(ConstVals.java:5)

Remove line 4 for the perfectly functional code.

Misinterpretted Use of Operators ( == vs .equals())

A lot of beginners start working with integers while learning the basics of a programming language. So it can be a challenge to remember that for string comparisons we use the “.equals()” method provided by Java and not == operator like in integers.

It's not a Wednesday! It's a Wednesday!

The output is contradictory because “today” and “thirdWeekDay” are referred to 2 different objects in the memory. However, the method “.equals()” compares the content stored in both arrays and returns true if it’s equal, false otherwise.

Accessing a non-static resource from a static method

If you want to access a non-static variable from a static method [let’s say the main method] then you need to create an instance of that object first. But if you fail to do that, java will get angry.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot make a static reference to the non-static field postalCode at topJavaErrors.NonStaticAccess.main(NonStaticAccess.java:17)

You can fix it, just by replacing line 9.

// Accessing the non-static member variable // by creating an instance of the object System.out.println("What's the postal code of your area? " + address.postalCode);

Conclusion

Programming errors are a part of the learning curve. Frequent errors might slow you down. But as a new programmer, it’s okay to learn things slowly. Now you’re familiar with some of the most common issues. Make sure you practise enough to get ahead of them. Happy coding and keep practising! 🙂

Источник

Common java program errors

  • The basics of TOGAF certification and some ways to prepare TOGAF offers architects a chance to learn the principles behind implementing an enterprise-grade software architecture, including.
  • Haskell vs. PureScript: The difference is complexity Haskell and PureScript each provide their own unique development advantages, so how should developers choose between these two .
  • A quick intro to the MACH architecture strategy While not particularly prescriptive, alignment with a MACH architecture strategy can help software teams ensure application .
  • Postman API platform will use Akita to tame rogue endpoints Akita’s discovery and observability will feed undocumented APIs into Postman’s design and testing framework to bring them into .
  • How to make use of specification-based test techniques Specification-based techniques can play a role in efficient test coverage. Choosing the right techniques can ensure thorough .
  • GitHub Copilot Chat aims to replace Googling for devs GitHub’s public beta of Copilot Chat rolls out GPT-4 integration that embeds a chat assistant into Visual Studio, but concerns .
  • Navigate multi-cloud billing challenges Keeping track of cloud bills from multiple clouds or accounts can be complex. Learn how to identify multi-cloud billing .
  • 5 Google Cloud cost optimization best practices Cost is always a top priority for enterprises. For those considering Google Cloud, or current users, discover these optimization .
  • How to create and manage Amazon EBS snapshots via AWS CLI EBS snapshots are an essential part of any data backup and recovery strategy in EC2-based deployments. Become familiar with how .
  • BrightTALK @ Black Hat USA 2022 BrightTALK’s virtual experience at Black Hat 2022 included live-streamed conversations with experts and researchers about the .
  • The latest from Black Hat USA 2023 Use this guide to Black Hat USA 2023 to keep up on breaking news and trending topics and to read expert insights on one of the .
  • API keys: Weaknesses and security best practices API keys are not a replacement for API security. They only offer a first step in authentication — and they require additional .
  • AWS Control Tower aims to simplify multi-account management Many organizations struggle to manage their vast collection of AWS accounts, but Control Tower can help. The service automates .
  • Break down the Amazon EKS pricing model There are several important variables within the Amazon EKS pricing model. Dig into the numbers to ensure you deploy the service .
  • Compare EKS vs. self-managed Kubernetes on AWS AWS users face a choice when deploying Kubernetes: run it themselves on EC2 or let Amazon do the heavy lifting with EKS. See .

Источник

Оцените статью