click on Guitar Chords Family
Software Engineering Tips
Friday, March 13, 2026
Saturday, September 30, 2023
How to Handle Null Pointer in Java
Handling null pointers in Java is an essential aspect of writing robust and error-free code.
Null pointer exceptions (NPEs) occur when you try to access or manipulate an object that is `null`.
Here are several strategies to handle null pointers in Java:
1. **Check for Null Before Accessing**:
- Always check if an object is `null` before accessing its properties or methods. You can use an `if` statement for this purpose.
if (myObject != null) {
// Access or manipulate myObject here
}
2. **Use the Safe Navigation Operator (Java 14+)**:
- Java 14 introduced the safe navigation operator (`?.`), which allows you to safely access properties and methods of an object without throwing an NPE if the object is `null`.
String result = myObject?.getValue(); // Safe access to getValue() without NPE
3. **Use Optional** (Java 8+):
- Consider using the `Optional` class to represent values that may be `null`. This can help you handle null cases more explicitly and avoid NPEs.
Optional<String> optionalValue = Optional.ofNullable(myObject);
optionalValue.ifPresent(value -> {
// Access or manipulate the value safely
});
4. **Default Values**:
- Provide default values or fallback values when dealing with potentially `null` objects.
String valueOrDefault = (myObject != null) ? myObject.getValue() : "Default";
5. **Try-Catch**:
- Use try-catch blocks to catch and handle NPEs when you can't prevent them entirely.
However, it's generally better to avoid NPEs by using the aforementioned techniques.
try {
// Code that may throw a NPE
} catch (NullPointerException e) {
// Handle the exception
}
6. **Prevent Nulls**:
- Whenever possible, design your code to avoid returning or accepting `null` values. Use empty collections (`Collections.emptyList()`, `Collections.emptySet()`) or other sentinel values instead.
7. **Linter/Static Analysis Tools**:
- Consider using linters and static analysis tools like FindBugs, PMD, or SpotBugs. These tools can help identify potential NPEs in your code.
8. **Logging and Debugging**:
- Use logging and debugging tools to trace the source of NPEs during development and
testing.
By following these best practices and writing code defensively, you can reduce the likelihood of encountering null pointer exceptions and ensure your Java applications are more robust.
Friday, September 29, 2023
MOST USEFUL TOP 7 JAVA 8 FEATURES
TOP 7 JAVA 8 FEATURES
Quick Revise
Java 8 introduced several significant features and enhancements to the Java programming language. Here are seven of the top features introduced in Java 8:
Lambda Expressions:
They simplify the syntax for writing functional interfaces and make code more readable and expressive.
Functional Interfaces:
Java 8 introduced the @FunctionalInterface annotation to denote interfaces with a single abstract method.
Functional interfaces are a key part of the functional programming support in Java 8, enabling the use of lambda expressions.
Streams:
The Stream API provides a new abstraction for working with sequences of data.
You can perform operations like filter, map, reduce, and collect on streams, making it easier to process collections of data in a functional and declarative style.
Default Methods:
Default methods allow you to add new methods to interfaces without breaking compatibility with existing implementations.
This feature is useful for evolving existing APIs while maintaining backward compatibility.
Method References:
Method references provide a shorthand notation for referring to methods by their names.
They are often used in conjunction with lambda expressions to make code more concise.
Optional:
The Optional class is introduced to handle the absence of a value.
It helps prevent null pointer exceptions by explicitly indicating whether a value is present or not.
New Date and Time API (java.time):
Java 8 introduced a modern date and time API to replace the outdated java.util.Date and java.util.Calendar classes.
The new API is more comprehensive and follows ISO and Gregorian standards.
These features revolutionized the way Java developers write code, making it more concise, expressive, and efficient, particularly for functional programming paradigms. Java 8's features laid the foundation for further enhancements in subsequent Java versions.
Tuesday, March 14, 2023
Command to run zookeeper and kafka
Download Kafka binary file then extract then go to kafka root folder and open the terminal in macbook.Then run below commands:
manojrajput@Manojs-MacBook-Pro kafka_2.13-3.4.0 % ~/kafka_2.13-3.4.0/bin/zookeeper-server-start.sh ~/kafka_2.13-3.4.0/config/zookeeper.properties
manojrajput@Manojs-MacBook-Pro kafka_2.13-3.4.0 % sh bin/kafka-server-start.sh config/server.properties
Friday, March 10, 2023
Adding Lombok in STS IDE
Using install new software, you can install any plugin.
STS--> help--> install new software
Sunday, March 5, 2023
Can I use two server simulatneously in Spring boot project?
Yes, it is possible to use two servers simultaneously in a Spring Boot project. Spring Boot is a flexible framework that supports multiple deployment options and server configurations.
One common approach is to use an application server, such as Tomcat or Jetty, as the primary server for your Spring Boot project. This server will handle incoming HTTP requests and provide a container for your application to run in.
However, you can also use additional servers for specific purposes. For example, you might use a separate server for handling WebSocket connections or for serving static assets like images or videos. In this case, you would typically configure your Spring Boot application to use both servers in parallel.
To use two servers in a Spring Boot project, you will need to configure your application appropriately. You can do this by adding dependencies for the additional servers to your pom.xml or build.gradle file, and then configuring the servers in your Spring Boot application configuration files.
It's worth noting that using multiple servers can add complexity to your application, so it's important to ensure that you have a good understanding of the various components and how they interact. Additionally, be sure to test your application thoroughly to ensure that everything is working as expected.
Saturday, March 4, 2023
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured
To resolve :
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured
Just add
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class }) works for me.I was getting same error I tried with @EnableAutoConfiguration(exclude=...) didn't work.
For those that are wondering where to add @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class }), as it has been asked by user as well. You need to add it to the main Application class which is under src>main>java. By default it is set to @SpringBootApplication
Guitar Chords
click on Guitar Chords Family
-
Handling null pointers in Java is an essential aspect of writing robust and error-free code. Null pointer exceptions (NPEs) occur when you ...
-
Yes, it is possible to use two servers simultaneously in a Spring Boot project. Spring Boot is a flexible framework that supports multiple...
-
To resolve : Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured Just...
