我写了这段代码
class DownloadFileFromURL extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(getContext());
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... f_url)
{
int count;
try
{
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream("/sdcard/downloadedfile.mp3");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1)
{
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}
catch (Exception e)
{
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress)
{
ViewHolder.progressbar.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String file_url) {
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Dismiss();
}
}, 1000L);
}
}
public void Dismiss(){
pDialog.dismiss();
}
对于Web应用程序,我收到此错误java.lang.NullPointerException:null 在com.example.demo.Controller.StudentsController.create(StudentsController.java:27)〜[classes /:na] 当我尝试使用其功能访问页面时,它不起作用只是给我这个错误是什么问题?此外,当我初始化IStudent Repository界面时,IntelliJ告诉我,即使我用它来创建方法,studentRepository也没有分配。
答案 0 :(得分:0)
您没有注入IStudentsRepository。使用@Autowired
您可以像这样使用Field Injection
@Autowired
private IStudentsRepository studentsRepository;
或者最好的方法是使用构造函数注入。
@Controller
public class StudentsController {
private ArrayList<student> students = new ArrayList<>();
private final IStudentsRepository studentsRepository;
@Autowired
public StudentsController(final IStudentsRepository studentsRepository) {
this.studentsRepository = studentsRepository;
}