【Java直接修改类,null引用?】教程文章相关的互联网学习教程文章

java – BufferedReader.ready()方法确保readLine()方法不返回NULL吗?【代码】

我有这样的代码使用BufferedReader读取文本文件:BufferedReader reader=null;try {reader = new BufferedReader(new FileReader("file1.txt"));while (reader.ready()) {final String line = reader.readLine();System.out.println("<"+line+">");} catch (..){...}它工作正常但Findbugs报告警告:NP_DEREFERENCE_OF_READLINE_VALUE : Theresult of invoking readLine() isdereferenced without checking to seeif the result is ...

java – 使用JDBC将null插入Integer列【代码】

我有一个SQL列PROTOCOL,它可以为空并且在表上有约束PROTOCOL IN (1, 2, 3)此外,因为它是可空的,我想设置并获取表中的空值 但我不能将setInt和getInt作为null.如何使用JDBC将null设置为nullsetInt(4,null);解决方法:尝试使用.pst.setNull(4, java.sql.Types.INTEGER); //pst is prepared statement instance.Interface PreparedStatement.setNull API Mapping of java.sql.Types to SQL types 附: :编辑以反映Java 8更新.

解决webservice(Java)中dao层注入为null问题【代码】

首先在webservice指定发布的路径类中实现 ServletContextListener, 例如:import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;public class ILockService implements ServletContextListener{  static IUserRecordService userRecordService;@Overridepublic void contextInitialized(ServletContextEvent sce) {userRecordService = WebApplicationContextUtils.getWebApplicationContex...

java – 未初始化的对象vs对象初始化为NULL【代码】

我在Java工作. 我通常会设置一些对象:public class Foo {private SomeObject someName;// do stuffpublic void someMethod() {if (this.someName != null) {// do some stuff}} }问题是:这个例子中的someName是否等于null,因为我可以可靠地为所有对象假设空检查未初始化的对象是否准确?解决方法:正确,未显式初始化的引用类型的静态和实例成员都由Java设置为null.同样的规则适用于数组成员. 从Java Language Specification,第4.1...

为什么我可以在Java中抛出null?【代码】

参见英文答案 > Why is “throw null” not creating a compilation error in Java? 4个运行时:public class WhatTheShoot {public static void main(String args[]){try {throw null;} catch (Exception e){System.out.println(e instanceof NullPointerException);System.out.println(e instanceof FileNotFoundException);}} }回应是:true false这对我来说相当惊人.我原本以为这会造成编...

java – 为什么BitmapFactory.decodeByteArray返回null?【代码】

这是简单的代码,而不是获得结果来设置位图,我得到null.谁能告诉我我在哪里犯了错误?String test = "test"; byte[] byteA = test.getBytes(); Bitmap bmp = BitmapFactory.decodeByteArray(byteA, 0, byteA.length); //<- I get null here ImageView image = (ImageView) findViewById(R.id.image); image.setImageBitmap(bmp);UPDATE 好的,所以我不能像我想的那样将文本转换为图像.这样怎么样?这会创建一个位图吗?Paint paint =...

java – Android Geocoder getFromLocationName始终返回null【代码】

我一直在尝试对字符串进行地理编码以获取其坐标,但我的程序总是崩溃,因为当我尝试使用getFromLocationName()时它返回null.我一直试图解决这个问题几个小时,但没有任何工作.这是我的代码public class MainActivity extends Activity {private GoogleMap mMap;List<Address> addresses;MarkerOptions miami;String myLocation = "Miami,Florida";protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceS...

[DEBUG] java中用Runtime调用python 简单程序输出null【代码】【图】

今天需要在java中调用python脚本,首先考虑的是java自带的Runtime 在ubuntu和win10下分别测试,发现win10报错java源代码@Testpublic void testRuntime() throws InterruptedException {Scanner input = new Scanner(System.in);// 在同一行输入两个数字,用空格分开,作为传入Python代码的命令行参数System.out.println("Enter two integers(e.g. 12 34): ");String integers = input.nextLine();String[] numbers = integers.spli...

java – 为什么getRealPath()在使用.war文件部署时返回null?【代码】

参见英文答案 > What does servletcontext.getRealPath(“/”) mean and when should I use it 4个getRealPath()返回本地系统中的实际路径,但在使用.war文件部署时返回null.<%@ page import="java.io.*" %> <%@ page contentType="text/html;charset=ISO-8859-1" %> <% int iLf = 10; char cLf = (char)iLf; String a= application.getResource("/"); //String myfile = application.getRealP...

java中的垃圾收集器 – 将对象设置为null【代码】

让我们假设,有一个Tree对象,具有根TreeNode对象,并且每个TreeNode都有leftNode和rightNode对象(例如BinaryTree对象) 如果我打电话:myTree = null;树中相关的TreeNode对象真的会发生什么?将垃圾收集,或者我必须设置树对象内的所有相关对象?解决方法:Java中的垃圾收集是基于“可达性”执行的. JLS将术语定义如下:“A reachable object is any object that can be accessed in any potential continuing computation from any liv...

java – Firebase Firestore上的ServerTimestamp始终为null【代码】

我正在尝试使用Firebase Firestore在Android客户端中添加时间戳字段. 根据documentation:Annotation used to mark a Date field to be populated with a servertimestamp. If a POJO being written contains null for a@ServerTimestamp-annotated field, it will be replaced with aserver-generated timestamp.但是当我尝试时:@ServerTimestamp Date serverTime = null; // I tried both java.util.Date and java.sql.Date//.....

java – 将null返回为int三元运算符允许的int,而不是if语句【代码】

让我们看看以下代码段中的简单Java代码:public class Main {private int temp() {return true ? null : 0;// No compiler error - the compiler allows a return value of null// in a method signature that returns an int.}private int same() {if (true) {return null;// The same is not possible with if,// and causes a compile-time error - incompatible types.} else {return 0;}}public static void main(String[] ar...

java – 避免!= null语句【代码】

我使用object!= null来避免NullPointerException. 有没有一个很好的替代品呢? 例如:if (someobject != null) {someobject.doCalc(); }当不知道对象是否为null时,这可以避免NullPointerException. 请注意,接受的答案可能已过期,请参阅https://stackoverflow.com/a/2386013/12943以获取更新的方法.解决方法:这对我来说听起来像是一个相当普遍的问题,初级到中级开发人员往往会在某些方面面临这样的问题:他们要么不知道,要么不信任...

java – Hibernate为保存实体后使用@Formula注释的实体属性返回null【代码】

我有简单的课程,如下所示:@Entity @Table(name = "post") public class Post {@Id@GeneratedValueLong id;@Column(name = "title")String title;@Formula("(select current_date())")Date currentDate;@OneToMany(mappedBy = "post")Set<PostComment> commentList = new HashSet<>(); }并希望在服务中更新此实体:@Service @Transactional public class PostService {private final PostRepository postRepository;public PostSer...

为什么JAVA 10中的var无法初始化为null?【代码】

我正在玩JAVA 10中引入的新功能,在那里我发现了一个非常有趣的事实,你不能用null声明一个变量. 一旦你写下一段代码,var a = null;它抛出一个错误:variable initializer is null现在,我们都知道我们不能将基本类型声明为null,所以下面的语句没有任何意义.int a = null;这意味着,如果开发人员正在使用null初始化var,那么它肯定希望在其中放置一个Object,而不是文字val.如果是这样,我的问题是 为什么编译器不认为它是Object var而是...