I ask for advice in the next question. Need to implement user registration. I decided to try the library Retrofit, but the application fell.

PS: do not judge strictly, I'm new) Here is the code

private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); private static final String TAG = "this"; private final String baseUrl = "http://u1938.blue.elastictech.org/api/users"; private Gson gson = new GsonBuilder().create(); private Retrofit retrofit = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create(gson)) .baseUrl(baseUrl) .build(); private Link parse = (Link) retrofit.create(List.class); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.registration_activity); Intent intent = getIntent(); intent.getExtras(); buttonRegistration2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (editPassword != editPassword2) { Toast.makeText(RegistrationActivity.this, "Пароли не совпадают. Повторите попытку", Toast.LENGTH_SHORT).show(); } else { Map<String, String> mapJson = new HashMap<String, String>(); mapJson.put("email", editEmail.getText().toString()); mapJson.put("name", editUserName.getText().toString()); mapJson.put("password", editPassword.getText().toString()); mapJson.put("contact_number", editNumber.getText().toString()); Call<Object> call = parse.parseMethod(mapJson); try { Response<Object> response = call.execute(); Map<String, String> map = gson.fromJson(response.body().toString(), Map.class); for (Map.Entry e : map.entrySet()) { System.out.println(e.getKey() + "" + e.getValue()); Log.e(TAG, "Object"); } } catch (IOException e) { e.printStackTrace(); } } } }); } 

Interface

 interface Link { @FormUrlEncoded @POST("/users") Call<Object> parseMethod(@FieldMap Map<String, String> map); } 

Mistake

 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.idrisov.mypost/com.example.idrisov.mypost.RegistrationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2849) 

    1 answer 1

    You forgot to initialize onCreate variable after setContentView(R.layout.registration_activity);

    Add buttonRegistration2 = findViewById(R.id.your_buttton_id); where your_buttton_id is the id specified for the button in registration_activity.xml