You cannot use the dereference (dot, '.') operator to access instance variables or call methods on an instance if that instance is null. Doing so will yield a NullPointerException.
It is common practice to use something you know to be non-null for string comparison. For example, "something".equals(stringThatMayBeNull).
You cannot use the dereference (dot, '.') operator to access instance variables or call methods on an instance if that instance is null. Doing so will yield a NullPointerException.
It is common practice to use something you know to be non-null for string comparison. For example, "something".equals(stringThatMayBeNull).
Use Objects.equals() to compare strings, or any other objects if you're using JDK 7 or later. It will handle nulls without throwing exceptions. See more here: how-do-i-compare-strings-in-java
And if you're not running JDK 7 or later you can copy the equals method from Objects like this:
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
Java comparison == is not null-safe?
Best Way to Check if a String is Null or Empty in Java - TestMu AI Community
java - Is "use "abc".equals(myString) instead of ...
java - Compare Strings avoiding NullPointerException - Stack Overflow
Videos
When I type
string1 == string2
IntelliJ tells me to switch to equals(), which it says is null-safe.
But is == operator not null-safe?
I tried null == "abc", "abc" == null, null == null, but they consistently gave me right false false true.
What am I missing here?
Using "abc".equals(...) is a reasonable precaution against a condition you didn't anticipate, but doesn't really solve any problems. Maybe this particular method doesn't care that myString is null (the string comparison returns false, but that's it), but what about elsewhere? Now there's this null value working its way through your logic that's not supposed to be there.
In this particular case, it might not matter, but in another, letting this invalid value pass through part of the logic could lead to an incorrect state later on (e.g. an address with the city missing). Fail early!
If myString should not be null, then write the code so that it can't be. Add contracts to your methods, or at least some in-method argument validation to ensure invalid values are dealt with before the method's logic ever executes. Then be sure your tests cover these cases.
One goal of programming is robust code, ie. code that doesn't die horribly whenever something unexpected crops up.
in case of if ("abc".equals(myString)), the if clause doesn't get executed if the string is null, so throwing an exception isn't necessary. Of course it could be caused by bugs in code, but those should be found during developing/testing, not in production, by the customer!
Actually, i would take "abc".equals(myString) as an indication that the programmer explicitly didn't care whether the string was null or not. In critical code, i'd expect a lot more explicit checking.
I am writing a program for Servlets and coming across an error:
java.lang.NullPointerException: Cannot invoke "String.equals(Object)" because "condition" is null.
What I am trying to here is to print the form as a response to the client if the terms and conditions are not met.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>
first
</servlet-name>
<servlet-class>
com.servlets.MyServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
first
</servlet-name>
<url-pattern>
/RegisterServlet
</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>index.html
<!DOCTYPE html>
<html>
<head>
<title>Form Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style >
.container{
width:40%;
border:1px solid black;
margin:auto;
padding:20px;
}
</style>
</head>
<body>
<div class="container">
<h1 style="text-align:center">My Form</h1>
<form action="RegisterServlet" method="post">
<table>
<tr>
<td>Enter your name</td>
<td><input type="text" name="user_name" placeholder="Enter here"></td>
</tr>
<tr>
<td>Enter the password:</td>
<td><input type="password" name="user_password" placeholder="Enetr the password"></td>
</tr>
<tr>
<td>Enter your email:</td>
<td><input type="email" name="user_email" placeholder="Enter your email"></td>
</tr>
<tr>
<td>Select Gender:</td>
<td><input type="radio" name="user-gender" value="male">Male <input type="radio" name="user-gender" value="female">Female</td>
</tr>
<tr>
<td>Select your course:</td>
<td>
<select name="user-course">
<option value="Java">Java</option>
<option value="C">C</option>
<option value="C++">C++</option>
<option value="PHP">Php</option>
</select>
</td>
</tr>
<tr>
<td style="text-align:center">
<input type="checkbox" value="checked" name="condition"/>
</td>
<td>
Agree terms and conditions
</td>
</tr>
<tr>
<td>
</td>
<td>
<button type="submit">Register</button>
<button type="reset">Reset</button>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>MyServlet.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
/**
*
* @author PC
*/
public class MyServlet extends HttpServlet {
/**
*
* @param request
* @param response
* @throws IOException
*/
@Override
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
response.setContentType("text/html");
// Writing the message on the web page
PrintWriter out = response.getWriter();
out.println("<h2>Welcome to servlet</h2>");
String name=request.getParameter("user_name");
String password=request.getParameter("user_password");
String email=request.getParameter("user_email");
String gender=request.getParameter("user_gender");
String course=request.getParameter("user_course");
String condition=request.getParameter("condition");
if(condition!=null){
if(condition.equals("checked")){
out.println("<h2>"+"Name:"+name+"</h2>");
out.println("Password:"+password);
out.println("Email:"+email);
out.println("Gender:"+gender);
out.println("Course:"+course);
}else{
out.println("The terms and conditions have not been agreed upon");
}
}
else{
out.println("The terms and conditions have not been agreed upon");
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);
}
}
}