Tag: nullpointerexception

空指针exceptionJava

我刚刚开始学习Java,并遇到了一个问题。 尝试运行Android应用程序时,调用方法makeResponse时会出现NullPointerException。 代码提取(本帖末尾附有完整代码): private String makeResponse(String input){ //This doesn’t work yet. I keep getting null pointer exceptions on the line beginning “int id” line, but can’t tell why. String response = “You picked “+input; if (sw==null){ response+=” and sw is null”; //This doesn’t activate } if (input==null){ response+=” and input is null”; //This doesn’t activate […]

为什么我没有得到NullPointerException?

可能重复: Java中空引用的静态字段 我知道静态方法是在类级别上。 所以我知道我不需要创建实例来调用静态方法。 但我也知道我可以调用静态方法LIKE一个实例方法。 这是我感到困惑的地方,因为我在从null对象调用静态方法时期望NullPointerException (如在调用实例方法中)。 我真的很感激为什么我错在这里期待NullPointerException一些解释。 以下是示例代码: public class SampleClass { public static int getSumStatic(int x, int y){ return x+y; } public int getDifferenceInstance(int x, int y){ return xy; } } public class TestClass { public static void main (String[] args){ SampleClass sc=null; System.out.println(SampleClass.getSumStatic(2, 2)); //as expected //I was expecting NullPointerException in the […]

JLabel不会显示图像 – NullPointerException

这是我的第一个Java GUI程序,实际上只是我的第二个java程序,所以请放轻松我:)我的程序是大量谷歌搜索和阅读java文档的结果。 我的问题是我有一张52张精灵表,我试图使用subImage将这些卡分别保存到Buffered Image数组,只是为了测试目的,在窗口中显示所有52。 文件位于我确定的正确目录中。 我相信我的问题在于我使用Jlabels,或者只是一个愚蠢的错误。 无论如何,这是我的类,它执行精灵表分割 package gui; import java.awt.GridLayout; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; public class crdimgs extends JPanel {/** * */ static final long serialVersionUID = 1L; public final int width = 10; public final int height = 20; public int rows = 13; public […]

带有android片段的java.lang.NullPointerException

我正在尝试在我的应用程序中实现Fragments。 当我更改标签(我使用抽屉布局)时出现问题,我收到java.lang.NullPointerException错误。 我测试了主要活动的代码并且运行顺利。 唯一的问题是我在片段上应用它。 这是代码: import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.*; public class FragW extends Fragment { Spinner spinwatt ; Button calculate; EditText wattEdit; EditText costEdit; TextView resultText; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragw,container,false); calculate = (Button) getView().findViewById(R.id.calculate); […]

System.console()在NetBeans中提供NullPointerException

我是Java的新手。 我有以下问题:方法readLine()或nextLine() , nextInt()等抛出exception: NullPointerException 。 我使用NetBeans IDE(如果重要的话)。 public static void Reading() { String qq; qq = System.console().readLine(); System.console().printf(qq); }

在null-reference上创建方法引用不会引发exception

是否有可能在Java中的null引用上创建方法引用? 这样做可能永远不会正确,但可能导致以后很难找到的错误: public class Test { void m() { } public static void main(String[] args) { Test test = null; Runnable fn = test::m; // no exception System.out.println(fn); // prints Test$$Lambda$1/791452441@1c20c684 fn.run(); // throws a null pointer exception } }

使用CustomListAdapter时出现NullPointerException

我在Android Appiclation中创建CustomListAdapter。 但是实现它时我得到这个错误NullPointerException 。 这是我的CustomListAdapter.java代码: /** * */ package com.fanjavaid.searchhttprequest.adapter; import java.util.List; import com.android.volley.toolbox.ImageLoader; import com.android.volley.toolbox.NetworkImageView; import com.fanjavaid.searchhttprequest.R; import com.fanjavaid.searchhttprequest.app.AppController; import com.fanjavaid.searchhttprequest.model.Menu; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; /** * @author fanjavaid * */ public class CustomListAdapter extends BaseAdapter { private Activity activity; private LayoutInflater inflater; private […]

在drools中运行helloworld时获取空指针exception

运行简单的helloworld示例drools项目时出现以下错误。 199 [main] ERROR org.drools.compiler.kie.builder.impl.KieContainerImpl – Unknown KieSession name: ksession-rules java.lang.NullPointerException at com.sample.DroolsTest.main(DroolsTest.java:24) 码: package com.sample; import org.kie.api.KieServices; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; /** * This is a sample class to launch a rule. */ public class DroolsTest { public static final void main(String[] args) { try { // load up the knowledge base KieServices ks […]

尝试检查权限时出现NullPointerException

我正在尝试检查用户是否启用了android.Manifest.permission.ACCESS_FINE_LOCATION。 运行应用程序时,出现NullPointerException错误。 包dtt.romano.rsrpechhulp; import android.app.Activity; import android.content.Context; import android.content.IntentSender; import android.content.pm.PackageManager; import android.location.Location; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.util.Log; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationServices; public class LocationProvider extends MainActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,LocationListener { public abstract interface LocationCallback { public void handleNewLocation(Location location); } public static final String TAG […]

为什么我的图标处理代码会抛出NullPointerException?

我为我的按钮添加了一个图像,但是当我运行该帧时,会抛出这个exception。为什么?请帮助我。 init: deps-jar: compile-single: run-single: Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException at javax.swing.ImageIcon.(ImageIcon.java:138) at ClientGUI.IdAndPasswordFrame.initComponents(IdAndPasswordFrame.java:91) at ClientGUI.IdAndPasswordFrame.(IdAndPasswordFrame.java:22) at ClientGUI.IdAndPasswordFrame$4.run(IdAndPasswordFrame.java:200) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEvent(EventQueue.java:597) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160) at java.awt.EventDispatchThread.run(EventDispatchThread.java:121) BUILD SUCCESSFUL (total time: 1 second) 第138行: public ImageIcon (URL location) { this(location, location.toExternalForm()); } line91: jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource(“/Images/yahoo_1.gif”))); // NOI18N 我使用这个糟糕的检查(Peter […]